Skip to content

Commit 50feba3

Browse files
authored
More smoke tests. (#470)
This commit adds additional smoke tests for Firestore, Database, and Storage. These tests verify permission denied behavior and update behavior. Simple set behavior was already present.
1 parent 1a1c97c commit 50feba3

File tree

4 files changed

+176
-3
lines changed

4 files changed

+176
-3
lines changed

smoke-tests/src/database/java/com/google/firebase/testing/database/DatabaseTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ public final class DatabaseTest {
4040

4141
@Rule public final ActivityTestRule<Activity> activity = new ActivityTestRule<>(Activity.class);
4242

43+
@Test
44+
public void setValueShouldFailWithPermissionDenied() throws Exception {
45+
FirebaseAuth auth = FirebaseAuth.getInstance();
46+
FirebaseDatabase database = FirebaseDatabase.getInstance();
47+
48+
auth.signOut();
49+
Thread.sleep(1000); // TODO(allisonbm92): Introduce a better way to reduce flakiness.
50+
DatabaseReference doc = database.getReference("restaurants").child(TestId.create());
51+
HashMap<String, Object> data = new HashMap<>();
52+
data.put("location", "Google DUB");
53+
54+
try {
55+
Task<?> setTask = doc.setValue(new HashMap<>(data));
56+
Tasks2.waitForFailure(setTask);
57+
58+
// Unfortunately, there's no good way to test that this has the correct error code, because
59+
// Database does not expose it through the task interface. Perhaps we could re-structure this
60+
// in the future.
61+
} finally {
62+
Tasks2.waitBestEffort(doc.removeValue());
63+
}
64+
}
65+
4366
@Test
4467
public void setValueShouldTriggerListenerWithNewlySetData() throws Exception {
4568
FirebaseAuth auth = FirebaseAuth.getInstance();
@@ -69,6 +92,43 @@ public void setValueShouldTriggerListenerWithNewlySetData() throws Exception {
6992
}
7093
}
7194

95+
@Test
96+
public void updateChildrenShouldTriggerListenerWithUpdatedData() throws Exception {
97+
FirebaseAuth auth = FirebaseAuth.getInstance();
98+
FirebaseDatabase database = FirebaseDatabase.getInstance();
99+
100+
auth.signOut();
101+
Task<?> signInTask = auth.signInWithEmailAndPassword("[email protected]", "password");
102+
Tasks2.waitForSuccess(signInTask);
103+
104+
DatabaseReference doc = database.getReference("restaurants").child(TestId.create());
105+
HashMap<String, Object> originalData = new HashMap<>();
106+
originalData.put("location", "Google NYC");
107+
108+
try {
109+
Task<?> setTask = doc.setValue(new HashMap<>(originalData));
110+
Tasks2.waitForSuccess(setTask);
111+
SnapshotListener listener = new SnapshotListener();
112+
doc.addListenerForSingleValueEvent(listener);
113+
114+
HashMap<String, Object> updateData = new HashMap<>();
115+
updateData.put("count", 412L);
116+
117+
Task<?> updateTask = doc.updateChildren(new HashMap<>(updateData));
118+
Task<DataSnapshot> snapshotTask = listener.toTask();
119+
Tasks2.waitForSuccess(updateTask);
120+
Tasks2.waitForSuccess(snapshotTask);
121+
122+
DataSnapshot result = snapshotTask.getResult();
123+
HashMap<String, Object> finalData = new HashMap<>();
124+
finalData.put("location", "Google NYC");
125+
finalData.put("count", 412L);
126+
assertThat(result.getValue()).isEqualTo(finalData);
127+
} finally {
128+
Tasks2.waitBestEffort(doc.removeValue());
129+
}
130+
}
131+
72132
private static class SnapshotListener implements ValueEventListener {
73133
private final TaskCompletionSource<DataSnapshot> taskFactory = new TaskCompletionSource<>();
74134

smoke-tests/src/firestore/java/com/google/firebase/testing/firestore/FirestoreTest.java

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ public final class FirestoreTest {
4141

4242
@Rule public final ActivityTestRule<Activity> activity = new ActivityTestRule<>(Activity.class);
4343

44+
@Test
45+
public void setShouldFailWithPermissionDenied() throws Exception {
46+
FirebaseAuth auth = FirebaseAuth.getInstance();
47+
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
48+
49+
auth.signOut();
50+
Thread.sleep(1000); // TODO(allisonbm92): Introduce a better means to reduce flakes.
51+
DocumentReference doc = firestore.collection("restaurants").document(TestId.create());
52+
try {
53+
HashMap<String, Object> data = new HashMap<>();
54+
data.put("popularity", 5000L);
55+
56+
Task<?> setTask = doc.set(new HashMap<>(data));
57+
Throwable failure = Tasks2.waitForFailure(setTask);
58+
FirebaseFirestoreException ex = (FirebaseFirestoreException) failure;
59+
60+
assertThat(ex.getCode()).isEqualTo(FirebaseFirestoreException.Code.PERMISSION_DENIED);
61+
} finally {
62+
Tasks2.waitBestEffort(doc.delete());
63+
}
64+
}
65+
4466
@Test
4567
public void setShouldTriggerListenerWithNewlySetData() throws Exception {
4668
FirebaseAuth auth = FirebaseAuth.getInstance();
@@ -71,15 +93,70 @@ public void setShouldTriggerListenerWithNewlySetData() throws Exception {
7193
}
7294
}
7395

96+
@Test
97+
public void updateShouldTriggerListenerWithUpdatedData() throws Exception {
98+
FirebaseAuth auth = FirebaseAuth.getInstance();
99+
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
100+
101+
auth.signOut();
102+
Task<?> signInTask = auth.signInWithEmailAndPassword("[email protected]", "password");
103+
Tasks2.waitForSuccess(signInTask);
104+
105+
DocumentReference doc = firestore.collection("restaurants").document(TestId.create());
106+
try {
107+
HashMap<String, Object> originalData = new HashMap<>();
108+
originalData.put("location", "Google NYC");
109+
110+
Task<?> setTask = doc.set(new HashMap<>(originalData));
111+
Tasks2.waitForSuccess(setTask);
112+
113+
SnapshotListener listener = new SnapshotListener(2);
114+
ListenerRegistration registration = doc.addSnapshotListener(listener);
115+
116+
try {
117+
HashMap<String, Object> updateData = new HashMap<>();
118+
updateData.put("priority", 5L);
119+
120+
Task<?> updateTask = doc.update(new HashMap<>(updateData));
121+
Task<DocumentSnapshot> snapshotTask = listener.toTask();
122+
Tasks2.waitForSuccess(updateTask);
123+
Tasks2.waitForSuccess(snapshotTask);
124+
125+
DocumentSnapshot result = snapshotTask.getResult();
126+
HashMap<String, Object> finalData = new HashMap<>();
127+
finalData.put("location", "Google NYC");
128+
finalData.put("priority", 5L);
129+
assertThat(result.getData()).isEqualTo(finalData);
130+
} finally {
131+
registration.remove();
132+
}
133+
} finally {
134+
Tasks2.waitBestEffort(doc.delete());
135+
}
136+
}
137+
74138
private static class SnapshotListener implements EventListener<DocumentSnapshot> {
75139
private final TaskCompletionSource<DocumentSnapshot> taskFactory = new TaskCompletionSource<>();
76140

141+
private int eventsUntilFinished;
142+
143+
SnapshotListener() {
144+
this.eventsUntilFinished = 1;
145+
}
146+
147+
SnapshotListener(int count) {
148+
this.eventsUntilFinished = count;
149+
}
150+
77151
@Override
78152
public void onEvent(DocumentSnapshot snapshot, FirebaseFirestoreException error) {
79153
if (error != null) {
80154
taskFactory.trySetException(error);
81-
} else if (snapshot.exists()) {
82-
taskFactory.trySetResult(snapshot);
155+
} else {
156+
if (eventsUntilFinished == 1) {
157+
taskFactory.trySetResult(snapshot);
158+
}
159+
eventsUntilFinished -= 1;
83160
}
84161
}
85162

smoke-tests/src/main/java/com/google/firebase/testing/common/Tasks2.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public final class Tasks2 {
2525

2626
private static final long BEST_EFFORT_DURATION = 10;
27-
private static final long WAIT_DURATION = 30;
27+
private static final long WAIT_DURATION = 40;
2828
private static final TimeUnit WAIT_UNIT = TimeUnit.SECONDS;
2929

3030
private Tasks2() {}
@@ -50,6 +50,22 @@ public static void waitBestEffort(Task<?> task) {
5050
}
5151
}
5252

53+
/**
54+
* Waits for the task to complete with a failure.
55+
*
56+
* <p>This method will block the current thread and return the resulting exception. An assertion
57+
* failure will be thrown if the task does not fail.
58+
*/
59+
public static Throwable waitForFailure(Task<?> task) throws Exception {
60+
try {
61+
Tasks.await(task, WAIT_DURATION, WAIT_UNIT);
62+
} catch (ExecutionException ex) {
63+
return ex.getCause();
64+
}
65+
66+
throw new AssertionError("Task did not fail");
67+
}
68+
5369
/**
5470
* Waits for the task to complete successfully.
5571
*

smoke-tests/src/storage/java/com/google/firebase/testing/storage/StorageTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.android.gms.tasks.Task;
2323
import com.google.firebase.auth.FirebaseAuth;
2424
import com.google.firebase.storage.FirebaseStorage;
25+
import com.google.firebase.storage.StorageException;
2526
import com.google.firebase.storage.StorageReference;
2627
import com.google.firebase.testing.common.Tasks2;
2728
import com.google.firebase.testing.common.TestId;
@@ -37,6 +38,25 @@ public final class StorageTest {
3738

3839
@Rule public final ActivityTestRule<Activity> activity = new ActivityTestRule<>(Activity.class);
3940

41+
@Test
42+
public void putShouldFailWithNotAuthorized() throws Exception {
43+
FirebaseAuth auth = FirebaseAuth.getInstance();
44+
FirebaseStorage storage = FirebaseStorage.getInstance();
45+
46+
auth.signOut();
47+
StorageReference blob = storage.getReference("restaurants").child(TestId.create());
48+
byte[] data = "Google NYC".getBytes(StandardCharsets.UTF_8);
49+
50+
try {
51+
Task<?> putTask = blob.putBytes(Arrays.copyOf(data, data.length));
52+
Throwable failure = Tasks2.waitForFailure(putTask);
53+
StorageException ex = (StorageException) failure;
54+
assertThat(ex.getErrorCode()).isEqualTo(StorageException.ERROR_NOT_AUTHORIZED);
55+
} finally {
56+
Tasks2.waitBestEffort(blob.delete());
57+
}
58+
}
59+
4060
@Test
4161
public void getShouldReturnNewlyPutData() throws Exception {
4262
FirebaseAuth auth = FirebaseAuth.getInstance();

0 commit comments

Comments
 (0)