This commit is contained in:
Kevin Ngo 2014-05-06 16:48:12 -07:00
Родитель 9168017984
Коммит 7738c7c9d7
5 изменённых файлов: 272 добавлений и 22 удалений

Просмотреть файл

@ -4,6 +4,7 @@ var _ = require('underscore');
var assert = a.assert;
var eq_ = a.eq_;
var eeq_ = a.eeq_;
var feq_ = a.feq_;
var mock = a.mock;
var cache = require('cache');
@ -67,7 +68,11 @@ test('cache purge', function(done) {
test('cache purge filter', function(done) {
mock(
'cache',
{},
{
settings: {
offline_cache_enabled: function () { return false; }
}
},
function(cache) {
var key = 'test2:';
var str = 'poop';
@ -243,4 +248,108 @@ test('cache deep rewrite on set', function(done) {
);
});
test('cache get_ttl', function(done) {
mock(
'cache',
{
settings: {
offline_cache_enabled: function () { return true; },
offline_cache_whitelist: {
'/api/v1/fireplace/consumer-info/': 60 * 60, // 1 hour in seconds
'/api/v1/fireplace/search/featured/': 60 * 60 * 6, // 6 hours
'/api/v1/apps/category/': 60 * 60 * 24 // 1 day
}
}
},
function (cache) {
eq_(cache.get_ttl('https://omg.org/api/v1/fireplace/consumer-info/'),
60 * 60 * 1000); // 1 hour in microseconds
eq_(cache.get_ttl('https://omg.org/api/v1/apps/category/'),
60 * 60 * 24 * 1000); // 1 hour in microseconds
eq_(cache.get_ttl('https://omg.org/api/v1/swag/yolo/foreva/'), null);
done();
}
);
});
test('cache flush_signed', function(done) {
mock(
'cache',
{
user: {
logged_in: function() { return true; },
get_setting: function(x) {},
get_token: function() { return 'SwaggasaurusRex';}
}
},
function (cache) {
var data = 'ratchet data';
var signed_url = 'https://omg.org/api/v1/app/yolo/?_user=SwaggasaurusRex';
cache.set(signed_url, data);
eq_(cache.get(signed_url), data);
var unsigned_url = 'https://omg.org/api/v1/app/swag/';
cache.set(unsigned_url, data);
eq_(cache.get(unsigned_url), data);
feq_(Object.keys(cache.cache).sort(), [unsigned_url, signed_url]);
// Calling this should clear all cache keys whose URLs contain
// `_user=<token>`.
cache.flush_signed();
feq_(Object.keys(cache.cache), [unsigned_url]);
done();
}
);
});
test('cache flush_expired', function(done) {
mock(
'cache',
{
settings: {
offline_cache_enabled: function () { return true; },
offline_cache_whitelist: {
'/api/v1/fireplace/consumer-info/': 60 * 60, // 1 hour in seconds
'/api/v1/fireplace/search/featured/': 60 * 60 * 6, // 6 hours
'/api/v1/apps/category/': 60 * 60 * 24 // 1 day
}
}
},
function (cache) {
// Both were just added and unexpired ...
cache.set('https://omg.org/api/v1/fireplace/consumer-info/', {
'__time': +new Date()
});
cache.set('https://omg.org/api/v1/fireplace/search/featured/', {
'__time': +new Date()
});
cache.flush_expired();
assert(cache.has('https://omg.org/api/v1/fireplace/consumer-info/'));
assert(cache.has('https://omg.org/api/v1/fireplace/search/featured/'));
// Neither has expired ...
cache.set('https://omg.org/api/v1/fireplace/consumer-info/', {
'__time': +new Date() - (60 * 59 * 1000) // 59 min ago in microseconds
});
cache.flush_expired();
assert(cache.has('https://omg.org/api/v1/fireplace/consumer-info/'));
assert(cache.has('https://omg.org/api/v1/fireplace/search/featured/'));
// One has expired!
cache.set('https://omg.org/api/v1/fireplace/consumer-info/', {
'__time': +new Date() - (60 * 65 * 1000) // 1 hr 5 min ago in microseconds
});
cache.flush_expired();
assert(!cache.has('https://omg.org/api/v1/fireplace/consumer-info/'));
assert(cache.has('https://omg.org/api/v1/fireplace/search/featured/'));
done();
}
);
});
})();

Просмотреть файл

@ -20,7 +20,12 @@ test('model invalid type', function(done, fail) {
test('model cast/lookup/purge', function(done, fail) {
mock(
'models',
{settings: {model_prototypes: {'dummy': 'id', 'dummy2': 'id'}}},
{
settings: {
offline_cache_enabled: function () { return false; },
model_prototypes: {'dummy': 'id', 'dummy2': 'id'}
}
},
function(models) {
var d1 = models('dummy');
var d2 = models('dummy2');
@ -57,7 +62,12 @@ test('model cast/lookup/purge', function(done, fail) {
test('model cast/lookup/delete', function(done, fail) {
mock(
'models',
{settings: {model_prototypes: {'dummy': 'id'}}},
{
settings: {
offline_cache_enabled: function () { return false; },
model_prototypes: {'dummy': 'id'}
}
},
function(models) {
var d1 = models('dummy');
d1.cast({
@ -79,7 +89,12 @@ test('model cast/lookup/delete', function(done, fail) {
test('model cast/lookup/delete val', function(done, fail) {
mock(
'models',
{settings: {model_prototypes: {'dummy': 'id'}}},
{
settings: {
offline_cache_enabled: function () { return false; },
model_prototypes: {'dummy': 'id'}
}
},
function(models) {
var d1 = models('dummy');
d1.cast({
@ -101,7 +116,12 @@ test('model cast/lookup/delete val', function(done, fail) {
test('model cast/uncast', function(done, fail) {
mock(
'models',
{settings: {model_prototypes: {'dummy': 'id'}}},
{
settings: {
offline_cache_enabled: function () { return false; },
model_prototypes: {'dummy': 'id'}
}
},
function(models) {
var d1 = models('dummy');
@ -125,7 +145,12 @@ test('model cast/uncast', function(done, fail) {
test('model cast/uncast lists', function(done, fail) {
mock(
'models',
{settings: {model_prototypes: {'dummy': 'id'}}},
{
settings: {
offline_cache_enabled: function () { return false; },
model_prototypes: {'dummy': 'id'}
}
},
function(models) {
var d1 = models('dummy');
@ -165,7 +190,10 @@ test('model get hit', function(done, fail) {
'models',
{
requests: {get: function(x) {return 'surprise! ' + x;}},
settings: {model_prototypes: {'dummy': 'id'}}
settings: {
offline_cache_enabled: function () { return false; },
model_prototypes: {'dummy': 'id'}
}
},
function(models) {
var d1 = models('dummy');
@ -190,7 +218,10 @@ test('model get miss', function(done, fail) {
'models',
{
requests: {get: function(x) {return 'surprise! ' + x;}},
settings: {model_prototypes: {'dummy': 'id'}}
settings: {
offline_cache_enabled: function () { return false; },
model_prototypes: {'dummy': 'id'}
}
},
function(models) {
var d1 = models('dummy');
@ -207,7 +238,10 @@ test('model get getter', function(done, fail) {
'models',
{
requests: {get: function(x) {return "not the droids you're looking for";}},
settings: {model_prototypes: {'dummy': 'id'}}
settings: {
offline_cache_enabled: function () { return false; },
model_prototypes: {'dummy': 'id'}
}
},
function(models) {
var d1 = models('dummy');
@ -224,7 +258,12 @@ test('model get getter', function(done, fail) {
test('model lookup by', function(done, fail) {
mock(
'models',
{settings: {model_prototypes: {'dummy': 'id'}}},
{
settings: {
offline_cache_enabled: function () { return false; },
model_prototypes: {'dummy': 'id'}
}
},
function(models) {
var d1 = models('dummy');
@ -250,7 +289,12 @@ test('model lookup by', function(done, fail) {
test('model lookup miss', function(done, fail) {
mock(
'models',
{settings: {model_prototypes: {'dummy': 'id'}}},
{
settings: {
offline_cache_enabled: function () { return false; },
model_prototypes: {'dummy': 'id'}
}
},
function(models) {
var d1 = models('dummy');
@ -270,7 +314,12 @@ test('model lookup miss', function(done, fail) {
test('model cast list', function(done, fail) {
mock(
'models',
{settings: {model_prototypes: {'dummy': 'id'}}},
{
settings: {
offline_cache_enabled: function () { return false; },
model_prototypes: {'dummy': 'id'}
}
},
function(models) {
var d1 = models('dummy');

12
src/tests/navigation.js Normal file
Просмотреть файл

@ -0,0 +1,12 @@
(function() {
var a = require('assert');
var assert = a.assert;
var eq_ = a.eq_;
var navigation = require('navigation');
test('navigation url extraction', function(done) {
eq_(navigation.extract_nav_url('/foo/bar?src=all-popular'), '/foo/bar');
eq_(navigation.extract_nav_url('/foo/bar?src=all-popular&q=bar'), '/foo/bar?q=bar');
done();
});
})();

Просмотреть файл

@ -77,12 +77,14 @@ test('api url', function(done, fail) {
{
capabilities: {firefoxOS: true, widescreen: function() { return false; }, touch: 'foo'},
routes_api: {'homepage': '/foo/homepage'},
routes_api_args: function() {return function() {return function() {return {foo: 'bar'};};};}, // Functions get pre-evaluated.
settings: {api_url: 'api:'}
settings: {
api_url: 'api:',
api_cdn_whitelist: {},
}
}, function(urls) {
var homepage_url = urls.api.url('homepage');
eq_(homepage_url.substr(0, 17), 'api:/foo/homepage');
contains(homepage_url, 'foo=bar');
contains(homepage_url, 'dev=firefoxos');
done();
},
fail
@ -95,8 +97,10 @@ test('api url signage', function(done, fail) {
{
capabilities: {firefoxOS: true, widescreen: function() { return false; }, touch: 'foo'},
routes_api: {'homepage': '/foo/homepage'},
routes_api_args: function() {return function() {return function() {return {foo: 'bar'};};};}, // Functions get pre-evaluated.
settings: {api_url: 'api:'},
settings: {
api_url: 'api:',
api_cdn_whitelist: {}
},
user: {
logged_in: function() { return true; },
get_setting: function(x) {},
@ -121,13 +125,59 @@ test('api url signage', function(done, fail) {
);
});
test('api user-defined carrier (via SIM)', function(done, fail) {
mock(
'urls',
{
capabilities: {firefoxOS: true, widescreen: function() { return false; }, touch: 'foo'},
user: {logged_in: function() {}, get_setting: function(x) {
return x == 'carrier_sim' && 'seavanaquaticcorp';
}}
}, function(urls) {
contains(urls.api.url('search'), 'carrier=seavanaquaticcorp');
done();
},
fail
);
});
test('api user-defined carrier+region (via SIM)', function(done, fail) {
mock(
'urls',
{
capabilities: {firefoxOS: true, widescreen: function() { return false; }, touch: 'foo'},
user: {
logged_in: function() {},
get_setting: function(x) {
switch(x) {
case 'carrier_sim':
return 'seavanaquaticcorp';
case 'region_sim':
return 'underwater';
}
}
}
}, function(urls) {
var url = urls.api.url('search');
contains(url, 'carrier=seavanaquaticcorp');
contains(url, 'region=underwater');
done();
},
fail
);
});
test('api url blacklist', function(done, fail) {
mock(
'urls',
{
capabilities: {firefoxOS: true, widescreen: function() { return false; }, touch: 'foo'},
routes_api: {'homepage': '/foo/homepage'},
settings: {api_url: 'api:', api_param_blacklist: ['region']}
settings: {
api_cdn_whitelist: {},
api_url: 'api:',
api_param_blacklist: ['region']
}
}, function(urls) {
var homepage_url = urls.api.url('homepage');
eq_(homepage_url.substr(0, 17), 'api:/foo/homepage');
@ -138,12 +188,45 @@ test('api url blacklist', function(done, fail) {
);
});
test('api url CDN whitelist', function(done, fail) {
mock(
'urls',
{
routes_api: {
'homepage': '/api/v1/homepage/',
'search': '/api/v1/fireplace/search/?swag=yolo'
},
settings: {
api_url: 'api:',
api_cdn_whitelist: {
'/api/v1/fireplace/search/': 60, // 1 minute
'/api/v1/fireplace/search/featured/': 60 * 2, // 2 minutes
},
media_url: 'http://cdn.so.fast.omg.org'
}
}, function(urls) {
var homepage_url = urls.api.url('homepage');
eq_(homepage_url.substr(0, 21), 'api:/api/v1/homepage/');
var search_url = urls.api.url('search');
eq_(search_url.substr(0, 51),
'http://cdn.so.fast.omg.org/api/v1/fireplace/search/');
done();
},
fail
);
});
test('api url params', function(done, fail) {
mock(
'urls',
{
routes_api: {'homepage': '/foo/asdf'},
settings: {api_url: 'api:'}
settings: {
api_url: 'api:',
api_cdn_whitelist: {}
}
}, function(urls) {
var homepage_url = urls.api.params('homepage', {q: 'poop'});
eq_(homepage_url.substr(0, 13), 'api:/foo/asdf');

Просмотреть файл

@ -135,9 +135,6 @@ test('translate', function(done) {
eq_(filters.translate({'blah': '3'}, dlobj, 'es-PD'), '3');
eq_(filters.translate({'foo': 'bar', 'en-US': '3'}, null, 'es-PD'), '3');
eq_(filters.translate({}, dlobj, 'es-PD'), '');
eq_(filters.translate('', dlobj, 'es-PD'), '');
eq_(filters.translate(null, dlobj, 'es-PD'), '');
eq_(filters.translate(undefined, dlobj, 'es-PD'), '');
done();
});