Skip to content

Commit 9f57c9a

Browse files
authored
fix: Using AuthorizedHttpClient for updating rules during tests (#590)
1 parent fe43bd0 commit 9f57c9a

File tree

5 files changed

+87
-98
lines changed

5 files changed

+87
-98
lines changed

src/test/java/com/google/firebase/database/integration/FirebaseDatabaseAuthTestIT.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.firebase.database.integration;
1818

19-
import static org.junit.Assert.assertEquals;
2019
import static org.junit.Assert.assertTrue;
2120

2221
import com.google.api.core.ApiFutureCallback;
@@ -33,8 +32,6 @@
3332
import com.google.firebase.database.TestHelpers;
3433
import com.google.firebase.database.ValueEventListener;
3534
import com.google.firebase.testing.IntegrationTestUtils;
36-
import com.google.firebase.testing.IntegrationTestUtils.AppHttpClient;
37-
import com.google.firebase.testing.IntegrationTestUtils.ResponseInfo;
3835
import com.google.firebase.testing.ServiceAccount;
3936
import com.google.firebase.testing.TestUtils;
4037
import java.io.IOException;
@@ -49,11 +46,11 @@
4946
import org.junit.Test;
5047

5148
public class FirebaseDatabaseAuthTestIT {
52-
49+
5350
private static FirebaseApp masterApp;
54-
51+
5552
@BeforeClass
56-
public static void setUpClass() throws IOException {
53+
public static void setUpClass() throws IOException {
5754
masterApp = IntegrationTestUtils.ensureDefaultApp();
5855
setDatabaseRules();
5956
}
@@ -74,7 +71,7 @@ public void testAuthWithValidCertificateCredential() throws InterruptedException
7471
assertWriteSucceeds(db.getReference());
7572
assertReadSucceeds(db.getReference());
7673
}
77-
74+
7875
@Test
7976
public void testAuthWithInvalidCertificateCredential() throws InterruptedException, IOException {
8077
FirebaseOptions options =
@@ -87,7 +84,7 @@ public void testAuthWithInvalidCertificateCredential() throws InterruptedExcepti
8784
// TODO: Ideally, we would find a way to verify the correct log output.
8885
assertWriteTimeout(db.getReference());
8986
}
90-
87+
9188
@Test
9289
public void testDatabaseAuthVariablesAuthorization() throws InterruptedException {
9390
Map<String, Object> authVariableOverrides = ImmutableMap.<String, Object>of(
@@ -110,7 +107,7 @@ public void testDatabaseAuthVariablesAuthorization() throws InterruptedException
110107
assertWriteSucceeds(testAuthOverridesDb.getReference("test-custom-field-only"));
111108
assertReadSucceeds(testAuthOverridesDb.getReference("test-custom-field-only"));
112109
}
113-
110+
114111
@Test
115112
public void testDatabaseAuthVariablesNoAuthorization() throws InterruptedException {
116113
FirebaseOptions options = masterApp.getOptions().toBuilder()
@@ -128,25 +125,25 @@ public void testDatabaseAuthVariablesNoAuthorization() throws InterruptedExcepti
128125
assertReadFails(testAuthOverridesDb.getReference("test-uid-only"));
129126
assertWriteFails(testAuthOverridesDb.getReference("test-custom-field-only"));
130127
assertReadFails(testAuthOverridesDb.getReference("test-custom-field-only"));
131-
assertWriteSucceeds(testAuthOverridesDb.getReference("test-noauth-only"));
128+
assertWriteSucceeds(testAuthOverridesDb.getReference("test-noauth-only"));
132129
}
133-
130+
134131
private static void assertWriteSucceeds(DatabaseReference ref) throws InterruptedException {
135132
doWrite(ref, /*shouldSucceed=*/ true, /*shouldTimeout=*/ false);
136133
}
137-
134+
138135
private static void assertWriteFails(DatabaseReference ref) throws InterruptedException {
139136
doWrite(ref, /*shouldSucceed=*/ false, /*shouldTimeout=*/ false);
140137
}
141-
138+
142139
private static void assertWriteTimeout(DatabaseReference ref) throws InterruptedException {
143140
doWrite(ref, /*shouldSucceed=*/ false, /*shouldTimeout=*/ true);
144141
}
145-
142+
146143
private static void assertReadSucceeds(DatabaseReference ref) throws InterruptedException {
147144
doRead(ref, /*shouldSucceed=*/ true, /*shouldTimeout=*/ false);
148145
}
149-
146+
150147
private static void assertReadFails(DatabaseReference ref) throws InterruptedException {
151148
doRead(ref, /*shouldSucceed=*/ false, /*shouldTimeout=*/ false);
152149
}
@@ -180,7 +177,7 @@ public void onSuccess(Void result) {
180177
assertTrue("Write successful (expected to fail).", !success.get());
181178
}
182179
}
183-
180+
184181
private static void doRead(
185182
DatabaseReference ref, final boolean shouldSucceed, final boolean shouldTimeout)
186183
throws InterruptedException {
@@ -211,9 +208,9 @@ public void onCancelled(DatabaseError databaseError) {
211208
assertTrue("Read successful (expected to fail).", !success.get());
212209
}
213210
}
214-
211+
215212
private static void setDatabaseRules() throws IOException {
216-
// TODO: Use more than uid in rule Set rules so the only allowed operation is writing to
213+
// TODO: Use more than uid in rule Set rules so the only allowed operation is writing to
217214
// /test-uid-only by user with uid 'test'.
218215
String rules =
219216
"{\n"
@@ -232,8 +229,7 @@ private static void setDatabaseRules() throws IOException {
232229
+ " }\n"
233230
+ "}";
234231

235-
AppHttpClient client = new AppHttpClient();
236-
ResponseInfo info = client.put("/.settings/rules.json", rules);
237-
assertEquals(200, info.getStatus());
232+
RulesClient client = new RulesClient();
233+
client.updateRules(rules);
238234
}
239235
}

src/test/java/com/google/firebase/database/integration/OrderByTestIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ private static String formatRules(DatabaseReference ref, String rules) {
8787
}
8888

8989
private static void uploadRules(FirebaseApp app, String rules) throws IOException {
90-
IntegrationTestUtils.AppHttpClient client = new IntegrationTestUtils.AppHttpClient(app);
91-
IntegrationTestUtils.ResponseInfo response = client.put("/.settings/rules.json", rules);
92-
assertEquals(200, response.getStatus());
90+
RulesClient client = new RulesClient(app);
91+
client.updateRules(rules);
9392
}
9493

9594
@Test
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.database.integration;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.api.client.http.ByteArrayContent;
22+
import com.google.api.client.http.GenericUrl;
23+
import com.google.api.client.http.HttpRequest;
24+
import com.google.api.client.http.HttpRequestFactory;
25+
import com.google.api.client.http.HttpResponse;
26+
import com.google.common.io.ByteStreams;
27+
import com.google.firebase.FirebaseApp;
28+
import com.google.firebase.internal.ApiClientUtils;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
32+
/**
33+
* A simple HTTP client wrapper for updating RTDB security rules.
34+
*/
35+
final class RulesClient {
36+
37+
private final String databaseUrl;
38+
private final HttpRequestFactory requestFactory;
39+
40+
RulesClient() {
41+
this(FirebaseApp.getInstance());
42+
}
43+
44+
RulesClient(FirebaseApp app) {
45+
this.databaseUrl = checkNotNull(app).getOptions().getDatabaseUrl();
46+
this.requestFactory = ApiClientUtils.newAuthorizedRequestFactory(
47+
app, /*retryConfig*/ null);
48+
}
49+
50+
public void updateRules(String json) throws IOException {
51+
String url = this.databaseUrl + "/.settings/rules.json";
52+
HttpRequest request = requestFactory.buildPutRequest(
53+
new GenericUrl(url),
54+
ByteArrayContent.fromString("application/json", json));
55+
HttpResponse response = request.execute();
56+
try {
57+
InputStream in = response.getContent();
58+
if (in != null) {
59+
ByteStreams.exhaust(in);
60+
}
61+
} finally {
62+
ApiClientUtils.disconnectQuietly(response);
63+
}
64+
}
65+
}

src/test/java/com/google/firebase/database/integration/RulesTestIT.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class RulesTestIT {
6666
MapBuilder.of("rules", MapBuilder.of(".read", "auth != null", ".write", "auth != null"));
6767

6868
private static final Map<String, Object> testRules;
69-
69+
7070
static {
7171
testRules = new MapBuilder()
7272
.put("read_only", MapBuilder.of(".read", true))
@@ -137,9 +137,8 @@ public void checkAndCleanupApp() {
137137
}
138138

139139
private static void uploadRules(String rules) throws IOException {
140-
IntegrationTestUtils.AppHttpClient client = new IntegrationTestUtils.AppHttpClient(masterApp);
141-
IntegrationTestUtils.ResponseInfo response = client.put("/.settings/rules.json", rules);
142-
assertEquals(200, response.getStatus());
140+
RulesClient client = new RulesClient(masterApp);
141+
client.updateRules(rules);
143142
}
144143

145144
@Test

src/test/java/com/google/firebase/testing/IntegrationTestUtils.java

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,12 @@
1616

1717
package com.google.firebase.testing;
1818

19-
import static com.google.common.base.Preconditions.checkNotNull;
20-
21-
import com.google.api.client.http.ByteArrayContent;
22-
import com.google.api.client.http.GenericUrl;
23-
import com.google.api.client.http.HttpRequest;
24-
import com.google.api.client.http.HttpRequestFactory;
25-
import com.google.api.client.http.HttpResponse;
2619
import com.google.api.client.json.GenericJson;
2720
import com.google.cloud.firestore.FirestoreOptions;
2821
import com.google.common.collect.ImmutableList;
29-
import com.google.common.io.ByteStreams;
3022
import com.google.common.io.CharStreams;
3123
import com.google.firebase.FirebaseApp;
3224
import com.google.firebase.FirebaseOptions;
33-
import com.google.firebase.TestOnlyImplFirebaseTrampolines;
3425
import com.google.firebase.database.DatabaseReference;
3526
import com.google.firebase.database.FirebaseDatabase;
3627
import com.google.firebase.internal.ApiClientUtils;
@@ -148,65 +139,4 @@ public static List<DatabaseReference> getRandomNode(FirebaseApp app, int count)
148139
}
149140
return builder.build();
150141
}
151-
152-
public static class AppHttpClient {
153-
154-
private final FirebaseApp app;
155-
private final FirebaseOptions options;
156-
private final HttpRequestFactory requestFactory;
157-
158-
public AppHttpClient() {
159-
this(FirebaseApp.getInstance());
160-
}
161-
162-
public AppHttpClient(FirebaseApp app) {
163-
this.app = checkNotNull(app);
164-
this.options = app.getOptions();
165-
this.requestFactory = this.options.getHttpTransport().createRequestFactory();
166-
}
167-
168-
public ResponseInfo put(String path, String json) throws IOException {
169-
String url = options.getDatabaseUrl() + path + "?access_token=" + getToken();
170-
HttpRequest request = requestFactory.buildPutRequest(new GenericUrl(url),
171-
ByteArrayContent.fromString("application/json", json));
172-
HttpResponse response = null;
173-
try {
174-
response = request.execute();
175-
return new ResponseInfo(response);
176-
} finally {
177-
if (response != null) {
178-
response.disconnect();
179-
}
180-
}
181-
}
182-
183-
private String getToken() {
184-
// TODO: We should consider exposing getToken (or similar) publicly for the
185-
// purpose of servers doing authenticated REST requests like this.
186-
return TestOnlyImplFirebaseTrampolines.getToken(app, false);
187-
}
188-
}
189-
190-
public static class ResponseInfo {
191-
private final int status;
192-
private final byte[] payload;
193-
194-
private ResponseInfo(HttpResponse response) throws IOException {
195-
this.status = response.getStatusCode();
196-
InputStream in = response.getContent();
197-
if (in != null) {
198-
this.payload = ByteStreams.toByteArray(in);
199-
} else {
200-
this.payload = new byte[0];
201-
}
202-
}
203-
204-
public int getStatus() {
205-
return status;
206-
}
207-
208-
public byte[] getPayload() {
209-
return payload;
210-
}
211-
}
212142
}

0 commit comments

Comments
 (0)