-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.go
140 lines (114 loc) · 4.15 KB
/
test_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package firebasetools
import (
"context"
"fmt"
"testing"
"cloud.google.com/go/firestore"
"firebase.google.com/go/auth"
"github.com/stretchr/testify/assert"
)
// GetAuthenticatedContext returns a logged in context, useful for test purposes
func GetAuthenticatedContext(t *testing.T) context.Context {
ctx := context.Background()
authToken := GetAuthToken(ctx, t)
authenticatedContext := context.WithValue(ctx, AuthTokenContextKey, authToken)
return authenticatedContext
}
// GetFirestoreClientTestUtil ...
func GetFirestoreClientTestUtil(t *testing.T) *firestore.Client {
fc := &FirebaseClient{}
firebaseApp, err := fc.InitFirebase()
assert.Nil(t, err)
ctx := GetAuthenticatedContext(t)
firestoreClient, err := firebaseApp.Firestore(ctx)
assert.Nil(t, err)
assert.NotNil(t, firestoreClient)
return firestoreClient
}
// GetAuthenticatedContextAndToken returns a logged in context and ID token.
// It is useful for test purposes
func GetAuthenticatedContextAndToken(t *testing.T) (context.Context, *auth.Token) {
ctx := context.Background()
authToken := GetAuthToken(ctx, t)
authenticatedContext := context.WithValue(ctx, AuthTokenContextKey, authToken)
return authenticatedContext, authToken
}
// GetAuthToken ...
func GetAuthToken(ctx context.Context, t *testing.T) *auth.Token {
authToken, _ := getAuthTokenAndBearerToken(ctx, t)
return authToken
}
func getAuthTokenAndBearerToken(ctx context.Context, t *testing.T) (*auth.Token, string) {
user, userErr := GetOrCreateFirebaseUser(ctx, TestUserEmail)
assert.Nil(t, userErr)
assert.NotNil(t, user)
customToken, tokenErr := CreateFirebaseCustomToken(ctx, user.UID)
assert.Nil(t, tokenErr)
assert.NotNil(t, customToken)
idTokens, idErr := AuthenticateCustomFirebaseToken(customToken)
assert.Nil(t, idErr)
assert.NotNil(t, idTokens)
bearerToken := idTokens.IDToken
authToken, err := ValidateBearerToken(ctx, bearerToken)
assert.Nil(t, err)
assert.NotNil(t, authToken)
return authToken, bearerToken
}
// GetOrCreateAnonymousUser creates an anonymous user
// For documentation and test purposes only
func GetOrCreateAnonymousUser(ctx context.Context) (*auth.UserRecord, error) {
authClient, err := GetFirebaseAuthClient(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get or create Firebase client: %w", err)
}
anonymousUserUID := "AgkGYKUsRifO2O9fTLDuVCMr2hb2" // This is an anonymous user
existingUser, userErr := authClient.GetUser(ctx, anonymousUserUID)
if userErr == nil {
return existingUser, nil
}
params := (&auth.UserToCreate{})
newUser, createErr := authClient.CreateUser(ctx, params)
if createErr != nil {
return nil, createErr
}
return newUser, nil
}
// GetAnonymousContext returns an anonymous logged in context, useful for test purposes
func GetAnonymousContext(t *testing.T) context.Context {
ctx := context.Background()
authToken := getAnonymousAuthToken(ctx, t)
authenticatedContext := context.WithValue(ctx, AuthTokenContextKey, authToken)
return authenticatedContext
}
func getAnonymousAuthToken(ctx context.Context, t *testing.T) *auth.Token {
user, userErr := GetOrCreateAnonymousUser(ctx)
assert.Nil(t, userErr)
assert.NotNil(t, user)
customToken, tokenErr := CreateFirebaseCustomToken(ctx, user.UID)
assert.Nil(t, tokenErr)
assert.NotNil(t, customToken)
idTokens, idErr := AuthenticateCustomFirebaseToken(customToken)
assert.Nil(t, idErr)
assert.NotNil(t, idTokens)
bearerToken := idTokens.IDToken
authToken, err := ValidateBearerToken(ctx, bearerToken)
assert.Nil(t, err)
assert.NotNil(t, authToken)
return authToken
}
// GetAuthenticatedContextFromUID creates an auth.Token given a valid uid
func GetAuthenticatedContextFromUID(ctx context.Context, uid string) (*auth.Token, error) {
customToken, err := CreateFirebaseCustomToken(ctx, uid)
if err != nil {
return nil, fmt.Errorf("failed to create an authenticated token: %w", err)
}
idTokens, err := AuthenticateCustomFirebaseToken(customToken)
if err != nil {
return nil, fmt.Errorf("failed to authenticated custom token: %w", err)
}
authToken, err := ValidateBearerToken(ctx, idTokens.IDToken)
if err != nil {
return nil, fmt.Errorf("failed to validate bearer token: %w", err)
}
return authToken, nil
}