14
14
import org .commcare .formplayer .auth .DjangoAuth ;
15
15
import org .commcare .formplayer .beans .AuthenticatedRequestBean ;
16
16
import org .commcare .formplayer .configuration .CacheConfiguration ;
17
+ import org .commcare .formplayer .junit .RestoreFactoryAnswer ;
18
+ import org .commcare .formplayer .sandbox .SqlStorage ;
19
+ import org .commcare .formplayer .sandbox .UserSqlSandbox ;
17
20
import org .commcare .formplayer .services .RestoreFactory ;
18
21
import org .commcare .formplayer .util .Constants ;
19
22
import org .commcare .formplayer .util .RequestUtils ;
20
23
import org .commcare .formplayer .utils .TestContext ;
24
+ import org .commcare .formplayer .util .UserUtils ;
21
25
import org .commcare .formplayer .utils .WithHqUser ;
22
26
import org .hamcrest .Description ;
23
27
import org .hamcrest .Matcher ;
51
55
*/
52
56
@ WebMvcTest
53
57
@ ContextConfiguration (classes = {TestContext .class , CacheConfiguration .class })
54
- public class RestoreFactoryTest {
58
+ public class RestoreFactoryTest extends BaseTestClass {
55
59
56
60
private static final String BASE_URL = "http://localhost:8000/a/restore-domain/phone/restore/" ;
57
61
private String username = "restore-dude" ;
@@ -74,6 +78,7 @@ public class RestoreFactoryTest {
74
78
public void setUp () throws Exception {
75
79
MockitoAnnotations .openMocks (this );
76
80
Mockito .reset (restoreFactorySpy );
81
+ Mockito .reset (restoreFactoryMock );
77
82
AuthenticatedRequestBean requestBean = new AuthenticatedRequestBean ();
78
83
requestBean .setRestoreAs (asUsername );
79
84
requestBean .setUsername (username );
@@ -311,6 +316,39 @@ public void testGetRequestHeaders_UseHmacAuthEvenIfHqAuthPresent() throws Except
311
316
);
312
317
}
313
318
319
+ @ Test
320
+ public void testChecksForLocationChanges () throws Exception {
321
+ RestoreFactoryAnswer beforeChange = new RestoreFactoryAnswer ("restores/location_update1.xml" );
322
+ Mockito .doAnswer (beforeChange ).when (restoreFactoryMock ).getRestoreXml (false );
323
+
324
+ UserSqlSandbox beforeSandbox = restoreFactoryMock .performTimedSync (false , false , false );
325
+ Assertions .assertFalse (restoreFactoryMock .getHasLocationChanged ());
326
+ Assertions .assertEquals ("testLocationId1" , UserUtils .getUserLocationsByDomain (domain , beforeSandbox ));
327
+
328
+ RestoreFactoryAnswer afterChange = new RestoreFactoryAnswer ("restores/location_update2.xml" );
329
+ Mockito .doAnswer (afterChange ).when (restoreFactoryMock ).getRestoreXml (false );
330
+
331
+ UserSqlSandbox afterSandbox = restoreFactoryMock .performTimedSync (false , false , false );
332
+ Assertions .assertTrue (restoreFactoryMock .getHasLocationChanged ());
333
+ Assertions .assertEquals ("testLocationId2" , UserUtils .getUserLocationsByDomain (domain , afterSandbox ));
334
+ }
335
+
336
+ @ Test
337
+ public void testSameLocationsDoesntFlagChange () throws Exception {
338
+ RestoreFactoryAnswer beforeChange = new RestoreFactoryAnswer ("restores/location_update3.xml" );
339
+ Mockito .doAnswer (beforeChange ).when (restoreFactoryMock ).getRestoreXml (false );
340
+
341
+ UserSqlSandbox beforeSandbox = restoreFactoryMock .performTimedSync (false , false , false );
342
+ Assertions .assertEquals ("testLocationId1 testLocationId2" , UserUtils .getUserLocationsByDomain (domain , beforeSandbox ));
343
+
344
+ RestoreFactoryAnswer afterChange = new RestoreFactoryAnswer ("restores/location_update4.xml" );
345
+ Mockito .doAnswer (afterChange ).when (restoreFactoryMock ).getRestoreXml (false );
346
+
347
+ UserSqlSandbox afterSandbox = restoreFactoryMock .performTimedSync (false , false , false );
348
+ Assertions .assertFalse (restoreFactoryMock .getHasLocationChanged ());
349
+ Assertions .assertEquals ("testLocationId2 testLocationId1" , UserUtils .getUserLocationsByDomain (domain , afterSandbox ));
350
+ }
351
+
314
352
private void validateHeaders (HttpHeaders headers ,
315
353
List <Matcher <Map <? extends String , ? extends List <String >>>> matchers ) {
316
354
for (Matcher <Map <? extends String , ? extends List <String >>> matcher : matchers ) {
0 commit comments