Skip to content

Commit 8be4851

Browse files
authored
Firestore API (#34)
* Prototyping GCS and Firestore APIs * Implemented Firestore and GCS APIs; Added unit and integration tests * Accepting a context argument in Auth(), Storage() and Firestore() * Removing ctx from internal.AuthConfig and internal.StorageConfig * Flagging Firestore integration test
1 parent 252b3bb commit 8be4851

File tree

5 files changed

+132
-4
lines changed

5 files changed

+132
-4
lines changed

firebase.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
package firebase
1919

2020
import (
21+
"errors"
22+
23+
"cloud.google.com/go/firestore"
24+
2125
"firebase.google.com/go/auth"
2226
"firebase.google.com/go/internal"
2327
"firebase.google.com/go/storage"
@@ -31,6 +35,8 @@ import (
3135
)
3236

3337
var firebaseScopes = []string{
38+
"https://www.googleapis.com/auth/cloud-platform",
39+
"https://www.googleapis.com/auth/datastore",
3440
"https://www.googleapis.com/auth/devstorage.full_control",
3541
"https://www.googleapis.com/auth/firebase",
3642
"https://www.googleapis.com/auth/userinfo.email",
@@ -72,6 +78,14 @@ func (a *App) Storage(ctx context.Context) (*storage.Client, error) {
7278
return storage.NewClient(ctx, conf)
7379
}
7480

81+
// Firestore returns a new instance of firestore.Client from the cloud.google.com/go package.
82+
func (a *App) Firestore(ctx context.Context) (*firestore.Client, error) {
83+
if a.projectID == "" {
84+
return nil, errors.New("project id is required to access Firestore")
85+
}
86+
return firestore.NewClient(ctx, a.projectID, a.opts...)
87+
}
88+
7589
// NewApp creates a new App from the provided config and client options.
7690
//
7791
// If the client options contain a valid credential (a service account file, a refresh token file or an

firebase_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,59 @@ func TestStorage(t *testing.T) {
228228
}
229229
}
230230

231+
func TestFirestore(t *testing.T) {
232+
ctx := context.Background()
233+
app, err := NewApp(ctx, nil, option.WithCredentialsFile("testdata/service_account.json"))
234+
if err != nil {
235+
t.Fatal(err)
236+
}
237+
238+
if c, err := app.Firestore(ctx); c == nil || err != nil {
239+
t.Errorf("Firestore() = (%v, %v); want (auth, nil)", c, err)
240+
}
241+
}
242+
243+
func TestFirestoreWithProjectID(t *testing.T) {
244+
varName := "GCLOUD_PROJECT"
245+
current := os.Getenv(varName)
246+
247+
if err := os.Setenv(varName, ""); err != nil {
248+
t.Fatal(err)
249+
}
250+
defer os.Setenv(varName, current)
251+
252+
ctx := context.Background()
253+
config := &Config{ProjectID: "project-id"}
254+
app, err := NewApp(ctx, config, option.WithCredentialsFile("testdata/refresh_token.json"))
255+
if err != nil {
256+
t.Fatal(err)
257+
}
258+
259+
if c, err := app.Firestore(ctx); c == nil || err != nil {
260+
t.Errorf("Firestore() = (%v, %v); want (auth, nil)", c, err)
261+
}
262+
}
263+
264+
func TestFirestoreWithNoProjectID(t *testing.T) {
265+
varName := "GCLOUD_PROJECT"
266+
current := os.Getenv(varName)
267+
268+
if err := os.Setenv(varName, ""); err != nil {
269+
t.Fatal(err)
270+
}
271+
defer os.Setenv(varName, current)
272+
273+
ctx := context.Background()
274+
app, err := NewApp(ctx, nil, option.WithCredentialsFile("testdata/refresh_token.json"))
275+
if err != nil {
276+
t.Fatal(err)
277+
}
278+
279+
if c, err := app.Firestore(ctx); c != nil || err == nil {
280+
t.Errorf("Firestore() = (%v, %v); want (nil, error)", c, err)
281+
}
282+
}
283+
231284
func TestCustomTokenSource(t *testing.T) {
232285
ctx := context.Background()
233286
ts := &testTokenSource{AccessToken: "mock-token-from-custom"}

integration/auth/auth_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626
"os"
2727
"testing"
2828

29+
"golang.org/x/net/context"
30+
2931
"firebase.google.com/go/auth"
3032
"firebase.google.com/go/integration/internal"
31-
32-
"golang.org/x/net/context"
3333
)
3434

3535
const idToolKitURL = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=%s"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2017 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package firestore
16+
17+
import (
18+
"context"
19+
"log"
20+
"reflect"
21+
"testing"
22+
23+
"firebase.google.com/go/integration/internal"
24+
)
25+
26+
func TestFirestore(t *testing.T) {
27+
if testing.Short() {
28+
log.Println("skipping Firestore integration tests in short mode.")
29+
return
30+
}
31+
ctx := context.Background()
32+
app, err := internal.NewTestApp(ctx)
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
37+
client, err := app.Firestore(ctx)
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
42+
doc := client.Collection("cities").Doc("Mountain View")
43+
data := map[string]interface{}{
44+
"name": "Mountain View",
45+
"country": "USA",
46+
"population": int64(77846),
47+
"capital": false,
48+
}
49+
if _, err := doc.Set(ctx, data); err != nil {
50+
t.Fatal(err)
51+
}
52+
snap, err := doc.Get(ctx)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
if !reflect.DeepEqual(snap.Data(), data) {
57+
t.Errorf("Get() = %v; want %v", snap.Data(), data)
58+
}
59+
if _, err := doc.Delete(ctx); err != nil {
60+
t.Fatal(err)
61+
}
62+
}

internal/internal.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
package internal
1717

1818
import (
19-
"google.golang.org/api/option"
20-
2119
"golang.org/x/oauth2/google"
20+
"google.golang.org/api/option"
2221
)
2322

2423
// AuthConfig represents the configuration of Firebase Auth service.

0 commit comments

Comments
 (0)