2018-01-19 03:59:58 +08:00
Cu . import ( "resource://services-sync/util.js" ) ;
Cu . import ( "resource://services-sync/record.js" ) ;
Cu . import ( "resource://services-sync/resource.js" ) ;
Cu . import ( "resource://testing-common/services/sync/fakeservices.js" ) ;
Cu . import ( "resource://testing-common/services/sync/utils.js" ) ;
Svc . DefaultPrefs . set ( "registerEngines" , "" ) ;
Cu . import ( "resource://services-sync/service.js" ) ;
// configure the identity we use for this test.
identityConfig = makeIdentityConfig ( { username : "johndoe" } ) ;
function FakeCollection ( ) {
this . deleted = false ;
}
FakeCollection . prototype = {
handler : function ( ) {
let self = this ;
return function ( request , response ) {
let body = "" ;
self . timestamp = new _timestamp ( ) ;
let timestamp = "" + self . timestamp ;
if ( request . method == "DELETE" ) {
body = timestamp ;
self . deleted = true ;
}
response . setHeader ( "X-Weave-Timestamp" , timestamp ) ;
response . setStatusLine ( request . httpVersion , 200 , "OK" ) ;
response . bodyOutputStream . write ( body , body . length ) ;
} ;
}
} ;
2018-10-06 06:57:51 +02:00
function setUpTestFixtures ( server ) {
2018-01-19 03:59:58 +08:00
let cryptoService = new FakeCryptoService ( ) ;
Service . serverURL = server . baseURI + "/" ;
Service . clusterURL = server . baseURI + "/" ;
yield configureIdentity ( identityConfig ) ;
}
function run _test ( ) {
initTestLogging ( "Trace" ) ;
run _next _test ( ) ;
}
function promiseStopServer ( server ) {
let deferred = Promise . defer ( ) ;
server . stop ( deferred . resolve ) ;
return deferred . promise ;
}
2018-10-06 06:57:51 +02:00
add _identity _test ( this , function test _wipeServer _list _success ( ) {
2018-01-19 03:59:58 +08:00
_ ( "Service.wipeServer() deletes collections given as argument." ) ;
let steam _coll = new FakeCollection ( ) ;
let diesel _coll = new FakeCollection ( ) ;
let server = httpd _setup ( {
"/1.1/johndoe/storage/steam" : steam _coll . handler ( ) ,
"/1.1/johndoe/storage/diesel" : diesel _coll . handler ( ) ,
"/1.1/johndoe/storage/petrol" : httpd _handler ( 404 , "Not Found" )
} ) ;
try {
yield setUpTestFixtures ( server ) ;
new SyncTestingInfrastructure ( server , "johndoe" , "irrelevant" , "irrelevant" ) ;
_ ( "Confirm initial environment." ) ;
do _check _false ( steam _coll . deleted ) ;
do _check _false ( diesel _coll . deleted ) ;
_ ( "wipeServer() will happily ignore the non-existent collection and use the timestamp of the last DELETE that was successful." ) ;
let timestamp = Service . wipeServer ( [ "steam" , "diesel" , "petrol" ] ) ;
do _check _eq ( timestamp , diesel _coll . timestamp ) ;
_ ( "wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted." ) ;
do _check _true ( steam _coll . deleted ) ;
do _check _true ( diesel _coll . deleted ) ;
} finally {
yield promiseStopServer ( server ) ;
Svc . Prefs . resetBranch ( "" ) ;
}
} ) ;
2018-10-06 06:57:51 +02:00
add _identity _test ( this , function test _wipeServer _list _503 ( ) {
2018-01-19 03:59:58 +08:00
_ ( "Service.wipeServer() deletes collections given as argument." ) ;
let steam _coll = new FakeCollection ( ) ;
let diesel _coll = new FakeCollection ( ) ;
let server = httpd _setup ( {
"/1.1/johndoe/storage/steam" : steam _coll . handler ( ) ,
"/1.1/johndoe/storage/petrol" : httpd _handler ( 503 , "Service Unavailable" ) ,
"/1.1/johndoe/storage/diesel" : diesel _coll . handler ( )
} ) ;
try {
yield setUpTestFixtures ( server ) ;
new SyncTestingInfrastructure ( server , "johndoe" , "irrelevant" , "irrelevant" ) ;
_ ( "Confirm initial environment." ) ;
do _check _false ( steam _coll . deleted ) ;
do _check _false ( diesel _coll . deleted ) ;
_ ( "wipeServer() will happily ignore the non-existent collection, delete the 'steam' collection and abort after an receiving an error on the 'petrol' collection." ) ;
let error ;
try {
Service . wipeServer ( [ "non-existent" , "steam" , "petrol" , "diesel" ] ) ;
do _throw ( "Should have thrown!" ) ;
} catch ( ex ) {
error = ex ;
}
_ ( "wipeServer() threw this exception: " + error ) ;
do _check _eq ( error . status , 503 ) ;
_ ( "wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted." ) ;
do _check _true ( steam _coll . deleted ) ;
do _check _false ( diesel _coll . deleted ) ;
} finally {
yield promiseStopServer ( server ) ;
Svc . Prefs . resetBranch ( "" ) ;
}
} ) ;
2018-10-06 06:57:51 +02:00
add _identity _test ( this , function test _wipeServer _all _success ( ) {
2018-01-19 03:59:58 +08:00
_ ( "Service.wipeServer() deletes all the things." ) ;
/ * *
* Handle the bulk DELETE request sent by wipeServer .
* /
let deleted = false ;
let serverTimestamp ;
function storageHandler ( request , response ) {
do _check _eq ( "DELETE" , request . method ) ;
do _check _true ( request . hasHeader ( "X-Confirm-Delete" ) ) ;
deleted = true ;
serverTimestamp = return _timestamp ( request , response ) ;
}
let server = httpd _setup ( {
"/1.1/johndoe/storage" : storageHandler
} ) ;
yield setUpTestFixtures ( server ) ;
_ ( "Try deletion." ) ;
new SyncTestingInfrastructure ( server , "johndoe" , "irrelevant" , "irrelevant" ) ;
let returnedTimestamp = Service . wipeServer ( ) ;
do _check _true ( deleted ) ;
do _check _eq ( returnedTimestamp , serverTimestamp ) ;
yield promiseStopServer ( server ) ;
Svc . Prefs . resetBranch ( "" ) ;
} ) ;
2018-10-06 06:57:51 +02:00
add _identity _test ( this , function test _wipeServer _all _404 ( ) {
2018-01-19 03:59:58 +08:00
_ ( "Service.wipeServer() accepts a 404." ) ;
/ * *
* Handle the bulk DELETE request sent by wipeServer . Returns a 404.
* /
let deleted = false ;
let serverTimestamp ;
function storageHandler ( request , response ) {
do _check _eq ( "DELETE" , request . method ) ;
do _check _true ( request . hasHeader ( "X-Confirm-Delete" ) ) ;
deleted = true ;
serverTimestamp = new _timestamp ( ) ;
response . setHeader ( "X-Weave-Timestamp" , "" + serverTimestamp ) ;
response . setStatusLine ( request . httpVersion , 404 , "Not Found" ) ;
}
let server = httpd _setup ( {
"/1.1/johndoe/storage" : storageHandler
} ) ;
yield setUpTestFixtures ( server ) ;
_ ( "Try deletion." ) ;
new SyncTestingInfrastructure ( server , "johndoe" , "irrelevant" , "irrelevant" ) ;
let returnedTimestamp = Service . wipeServer ( ) ;
do _check _true ( deleted ) ;
do _check _eq ( returnedTimestamp , serverTimestamp ) ;
yield promiseStopServer ( server ) ;
Svc . Prefs . resetBranch ( "" ) ;
} ) ;
2018-10-06 06:57:51 +02:00
add _identity _test ( this , function test _wipeServer _all _503 ( ) {
2018-01-19 03:59:58 +08:00
_ ( "Service.wipeServer() throws if it encounters a non-200/404 response." ) ;
/ * *
* Handle the bulk DELETE request sent by wipeServer . Returns a 503.
* /
function storageHandler ( request , response ) {
do _check _eq ( "DELETE" , request . method ) ;
do _check _true ( request . hasHeader ( "X-Confirm-Delete" ) ) ;
response . setStatusLine ( request . httpVersion , 503 , "Service Unavailable" ) ;
}
let server = httpd _setup ( {
"/1.1/johndoe/storage" : storageHandler
} ) ;
yield setUpTestFixtures ( server ) ;
_ ( "Try deletion." ) ;
let error ;
try {
new SyncTestingInfrastructure ( server , "johndoe" , "irrelevant" , "irrelevant" ) ;
Service . wipeServer ( ) ;
do _throw ( "Should have thrown!" ) ;
} catch ( ex ) {
error = ex ;
}
do _check _eq ( error . status , 503 ) ;
yield promiseStopServer ( server ) ;
Svc . Prefs . resetBranch ( "" ) ;
} ) ;
2018-10-06 06:57:51 +02:00
add _identity _test ( this , function test _wipeServer _all _connectionRefused ( ) {
2018-01-19 03:59:58 +08:00
_ ( "Service.wipeServer() throws if it encounters a network problem." ) ;
let server = httpd _setup ( { } ) ;
yield setUpTestFixtures ( server ) ;
Service . serverURL = "http://localhost:4352/" ;
Service . clusterURL = "http://localhost:4352/" ;
_ ( "Try deletion." ) ;
try {
Service . wipeServer ( ) ;
do _throw ( "Should have thrown!" ) ;
} catch ( ex ) {
do _check _eq ( ex . result , Cr . NS _ERROR _CONNECTION _REFUSED ) ;
}
Svc . Prefs . resetBranch ( "" ) ;
yield promiseStopServer ( server ) ;
} ) ;