Skip to content

Commit c17ddfb

Browse files
committed
Add test for saving current user data in secure store
1 parent d88b9a0 commit c17ddfb

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package io.skygear.skygear;
2+
3+
import android.annotation.SuppressLint;
4+
import android.content.Context;
5+
import android.content.SharedPreferences;
6+
import android.support.test.InstrumentationRegistry;
7+
import android.support.test.runner.AndroidJUnit4;
8+
9+
import org.json.JSONObject;
10+
import org.junit.After;
11+
import org.junit.AfterClass;
12+
import org.junit.Before;
13+
import org.junit.BeforeClass;
14+
import org.junit.Test;
15+
import org.junit.runner.RunWith;
16+
17+
import java.util.HashMap;
18+
import java.util.Map;
19+
20+
import static junit.framework.Assert.assertEquals;
21+
import static junit.framework.Assert.assertNotNull;
22+
import static junit.framework.Assert.assertNull;
23+
24+
@RunWith(AndroidJUnit4.class)
25+
@SuppressLint("CommitPrefEdits")
26+
public class SecurePersistentStoreUnitTest {
27+
static Context instrumentationContext;
28+
29+
private static void clearSharedPreferences(Context context) {
30+
SharedPreferences pref = context.getSharedPreferences(
31+
PersistentStore.SKYGEAR_PREF_SPACE,
32+
Context.MODE_PRIVATE
33+
);
34+
SharedPreferences.Editor edit = pref.edit();
35+
edit.clear();
36+
edit.commit();
37+
}
38+
39+
@BeforeClass
40+
public static void setUpClass() throws Exception {
41+
instrumentationContext = InstrumentationRegistry.getContext().getApplicationContext();
42+
clearSharedPreferences(instrumentationContext);
43+
}
44+
45+
@AfterClass
46+
public static void tearDownClass() throws Exception {
47+
clearSharedPreferences(instrumentationContext);
48+
instrumentationContext = null;
49+
}
50+
51+
@Before
52+
public void setUp() throws Exception {
53+
clearSharedPreferences(instrumentationContext);
54+
}
55+
56+
@After
57+
public void tearDown() throws Exception {
58+
clearSharedPreferences(instrumentationContext);
59+
}
60+
61+
@Test
62+
public void testPersistentStoreRestoreUserFromEmptyState() throws Exception {
63+
SecurePersistentStore persistentStore = new SecurePersistentStore(instrumentationContext);
64+
assertNull(persistentStore.currentUser);
65+
}
66+
67+
@Test
68+
public void testPersistentStoreSaveAndRestoreUser() throws Exception {
69+
SecurePersistentStore persistentStore = new SecurePersistentStore(instrumentationContext);
70+
71+
// Save user
72+
Map profile = new HashMap<>();
73+
profile.put("username", "user_12345");
74+
profile.put("email", "user12345@skygear.dev");
75+
76+
persistentStore.currentUser = new Record("user", "12345", profile);
77+
persistentStore.accessToken = "token_12345";
78+
persistentStore.save();
79+
80+
// Restore current user
81+
SecurePersistentStore newPersistentStore = new SecurePersistentStore(instrumentationContext);
82+
Record currentUser = newPersistentStore.currentUser;
83+
84+
assertEquals("user", currentUser.type);
85+
assertEquals("12345", currentUser.id);
86+
assertEquals("user_12345", currentUser.get("username"));
87+
assertEquals("user12345@skygear.dev", currentUser.get("email"));
88+
89+
assertEquals("token_12345", persistentStore.accessToken);
90+
91+
// Current user information should be store in encrypted fields
92+
SharedPreferences pref = instrumentationContext.getSharedPreferences(
93+
SecurePersistentStore.SKYGEAR_PREF_SPACE,
94+
Context.MODE_PRIVATE
95+
);
96+
97+
String currentUserString = pref.getString(PersistentStore.CURRENT_USER_KEY, null);
98+
String currentUserEncryptedString = pref.getString(
99+
SecurePersistentStore.CURRENT_USER_ENCRYPTED_KEY, null);
100+
String currentUserEncryptedKey = pref.getString(
101+
SecurePersistentStore.CURRENT_USER_ENCRYPTED_KEY_KEY, null);
102+
assertNull(currentUserString);
103+
assertNotNull(currentUserEncryptedString);
104+
assertNotNull(currentUserEncryptedKey);
105+
106+
String accessToken = pref.getString(PersistentStore.ACCESS_TOKEN_KEY, null);
107+
String accessTokenEncrypted = pref.getString
108+
(SecurePersistentStore.ACCESS_TOKEN_ENCRYPTED_KEY, null);
109+
String accessTokenEncryptedKey = pref.getString(
110+
SecurePersistentStore.ACCESS_TOKEN_ENCRYPTED_KEY_KEY, null);
111+
assertNull(accessToken);
112+
assertNotNull(accessTokenEncrypted);
113+
assertNotNull(accessTokenEncryptedKey);
114+
}
115+
}

skygear/src/main/java/io/skygear/skygear/Container.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ public void configure(Configuration config) {
146146
}
147147

148148
this.config = config;
149+
this.configPersistentStore(config);
149150
this.requestManager.configure(config);
150151
this.pubsub.configure(config);
151-
this.configPersistentStore(config);
152152
}
153153

154154
/**

0 commit comments

Comments
 (0)