2010-08-05 16:24:45 +00:00
Cu . import ( "resource://services-sync/util.js" ) ;
2011-01-19 00:23:30 +00:00
Cu . import ( "resource://services-sync/record.js" ) ;
2010-08-05 16:24:45 +00:00
Cu . import ( "resource://services-sync/resource.js" ) ;
2012-09-24 20:45:49 +00:00
Cu . import ( "resource://testing-common/services/sync/fakeservices.js" ) ;
Cu . import ( "resource://testing-common/services/sync/utils.js" ) ;
2010-08-05 16:24:45 +00:00
2010-10-18 22:10:22 +00:00
Svc . DefaultPrefs . set ( "registerEngines" , "" ) ;
Cu . import ( "resource://services-sync/service.js" ) ;
2010-08-05 16:24:45 +00:00
function FakeCollection ( ) {
this . deleted = false ;
}
FakeCollection . prototype = {
handler : function ( ) {
let self = this ;
return function ( request , response ) {
let body = "" ;
2011-10-28 05:25:00 +00:00
self . timestamp = new _timestamp ( ) ;
let timestamp = "" + self . timestamp ;
2010-08-05 16:24:45 +00:00
if ( request . method == "DELETE" ) {
2011-10-28 05:25:00 +00:00
body = timestamp ;
2010-08-05 16:24:45 +00:00
self . deleted = true ;
}
2011-10-28 05:25:00 +00:00
response . setHeader ( "X-Weave-Timestamp" , timestamp ) ;
2010-08-05 16:24:45 +00:00
response . setStatusLine ( request . httpVersion , 200 , "OK" ) ;
response . bodyOutputStream . write ( body , body . length ) ;
} ;
}
} ;
function setUpTestFixtures ( ) {
let cryptoService = new FakeCryptoService ( ) ;
2012-02-06 17:50:11 +00:00
Service . serverURL = TEST _SERVER _URL ;
Service . clusterURL = TEST _CLUSTER _URL ;
2012-03-22 22:49:50 +00:00
setBasicCredentials ( "johndoe" , null , "aabcdeabcdeabcdeabcdeabcde" ) ;
2010-08-05 16:24:45 +00:00
}
2011-10-28 05:25:00 +00:00
function run _test ( ) {
initTestLogging ( "Trace" ) ;
run _next _test ( ) ;
}
add _test ( function test _wipeServer _list _success ( ) {
2010-08-25 22:49:45 +00:00
_ ( "Service.wipeServer() deletes collections given as argument." ) ;
2010-08-05 16:24:45 +00:00
let steam _coll = new FakeCollection ( ) ;
let diesel _coll = new FakeCollection ( ) ;
let server = httpd _setup ( {
2011-04-08 21:52:08 +00:00
"/1.1/johndoe/storage/steam" : steam _coll . handler ( ) ,
2011-10-28 05:25:00 +00:00
"/1.1/johndoe/storage/diesel" : diesel _coll . handler ( ) ,
"/1.1/johndoe/storage/petrol" : httpd _handler ( 404 , "Not Found" )
} ) ;
try {
setUpTestFixtures ( ) ;
2012-03-22 22:49:50 +00:00
new SyncTestingInfrastructure ( "johndoe" , "irrelevant" , "irrelevant" ) ;
2011-10-28 05:25:00 +00:00
_ ( "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 {
server . stop ( run _next _test ) ;
Svc . Prefs . resetBranch ( "" ) ;
}
} ) ;
add _test ( function test _wipeServer _list _503 ( ) {
_ ( "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" ) ,
2011-04-08 21:52:08 +00:00
"/1.1/johndoe/storage/diesel" : diesel _coll . handler ( )
2010-08-05 16:24:45 +00:00
} ) ;
try {
setUpTestFixtures ( ) ;
2012-03-22 22:49:50 +00:00
new SyncTestingInfrastructure ( "johndoe" , "irrelevant" , "irrelevant" ) ;
2010-08-05 16:24:45 +00:00
_ ( "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 {
2010-08-25 22:49:45 +00:00
Service . wipeServer ( [ "non-existent" , "steam" , "petrol" , "diesel" ] ) ;
2011-10-28 05:25:00 +00:00
do _throw ( "Should have thrown!" ) ;
2010-08-05 16:24:45 +00:00
} catch ( ex ) {
error = ex ;
}
_ ( "wipeServer() threw this exception: " + error ) ;
2011-10-28 05:25:00 +00:00
do _check _eq ( error . status , 503 ) ;
2010-08-05 16:24:45 +00:00
_ ( "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 {
2011-10-28 05:25:00 +00:00
server . stop ( run _next _test ) ;
2010-08-05 16:24:45 +00:00
Svc . Prefs . resetBranch ( "" ) ;
}
2011-10-28 05:25:00 +00:00
} ) ;
2011-10-12 20:53:13 +00:00
2011-10-28 05:25:00 +00:00
add _test ( function test _wipeServer _all _success ( ) {
_ ( "Service.wipeServer() deletes all the things." ) ;
2012-08-29 21:43:41 +00:00
2011-10-28 05:25:00 +00:00
/ * *
* 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 ) ;
2011-10-12 20:53:13 +00:00
}
let server = httpd _setup ( {
2011-10-28 05:25:00 +00:00
"/1.1/johndoe/storage" : storageHandler
} ) ;
setUpTestFixtures ( ) ;
_ ( "Try deletion." ) ;
2012-03-22 22:49:50 +00:00
new SyncTestingInfrastructure ( "johndoe" , "irrelevant" , "irrelevant" ) ;
2011-10-28 05:25:00 +00:00
let returnedTimestamp = Service . wipeServer ( ) ;
do _check _true ( deleted ) ;
do _check _eq ( returnedTimestamp , serverTimestamp ) ;
server . stop ( run _next _test ) ;
Svc . Prefs . resetBranch ( "" ) ;
} ) ;
add _test ( function test _wipeServer _all _404 ( ) {
_ ( "Service.wipeServer() accepts a 404." ) ;
2012-08-29 21:43:41 +00:00
2011-10-28 05:25:00 +00:00
/ * *
* 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
2011-10-12 20:53:13 +00:00
} ) ;
2011-10-28 05:25:00 +00:00
setUpTestFixtures ( ) ;
2011-10-12 20:53:13 +00:00
2011-10-28 05:25:00 +00:00
_ ( "Try deletion." ) ;
2012-03-22 22:49:50 +00:00
new SyncTestingInfrastructure ( "johndoe" , "irrelevant" , "irrelevant" ) ;
2011-10-28 05:25:00 +00:00
let returnedTimestamp = Service . wipeServer ( ) ;
do _check _true ( deleted ) ;
do _check _eq ( returnedTimestamp , serverTimestamp ) ;
server . stop ( run _next _test ) ;
Svc . Prefs . resetBranch ( "" ) ;
} ) ;
add _test ( function test _wipeServer _all _503 ( ) {
_ ( "Service.wipeServer() throws if it encounters a non-200/404 response." ) ;
2012-08-29 21:43:41 +00:00
2011-10-28 05:25:00 +00:00
/ * *
* 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
} ) ;
setUpTestFixtures ( ) ;
_ ( "Try deletion." ) ;
let error ;
2010-11-30 00:41:33 +00:00
try {
2012-03-22 22:49:50 +00:00
new SyncTestingInfrastructure ( "johndoe" , "irrelevant" , "irrelevant" ) ;
2011-10-28 05:25:00 +00:00
Service . wipeServer ( ) ;
do _throw ( "Should have thrown!" ) ;
} catch ( ex ) {
error = ex ;
}
do _check _eq ( error . status , 503 ) ;
2011-10-12 20:53:13 +00:00
2011-10-28 05:25:00 +00:00
server . stop ( run _next _test ) ;
Svc . Prefs . resetBranch ( "" ) ;
} ) ;
add _test ( function test _wipeServer _all _connectionRefused ( ) {
_ ( "Service.wipeServer() throws if it encounters a network problem." ) ;
setUpTestFixtures ( ) ;
_ ( "Try deletion." ) ;
try {
2010-11-30 00:41:33 +00:00
Service . wipeServer ( ) ;
2011-10-28 05:25:00 +00:00
do _throw ( "Should have thrown!" ) ;
} catch ( ex ) {
do _check _eq ( ex . result , Cr . NS _ERROR _CONNECTION _REFUSED ) ;
2010-11-30 00:41:33 +00:00
}
2011-10-28 05:25:00 +00:00
run _next _test ( ) ;
Svc . Prefs . resetBranch ( "" ) ;
} ) ;