16
16
17
17
import { expect } from 'chai' ;
18
18
import * as firebase from '../src/api' ;
19
+ import { base64 } from '@firebase/util' ;
19
20
20
21
describe ( 'Testing Module Tests' , function ( ) {
21
22
it ( 'assertSucceeds() iff success' , async function ( ) {
@@ -46,42 +47,56 @@ describe('Testing Module Tests', function() {
46
47
} ) ;
47
48
} ) ;
48
49
49
- it ( 'initializeAdminApp() throws if no databaseName' , function ( ) {
50
- expect ( firebase . initializeAdminApp . bind ( null , { } ) ) . to . throw (
51
- / d a t a b a s e N a m e n o t s p e c i f i e d /
50
+ it ( 'initializeTestApp() with DatabaseAppOptions uses specified auth.' , async function ( ) {
51
+ let app = firebase . initializeTestApp ( {
52
+ projectId : 'foo' ,
53
+ auth : { }
54
+ } ) ;
55
+ let token = await ( app as any ) . INTERNAL . getToken ( ) ;
56
+ expect ( token ) . to . have . any . keys ( 'accessToken' ) ;
57
+ let claims = base64 . decodeString (
58
+ token . accessToken . split ( '.' ) [ 1 ] ,
59
+ /*webSafe=*/ false
52
60
) ;
53
- expect (
54
- firebase . initializeAdminApp . bind ( null , { databaseName : 'foo' } )
55
- ) . to . not . throw ( ) ;
56
- } ) ;
61
+ expect ( claims ) . to . equal ( '{}' ) ;
57
62
58
- it ( 'initializeAdminApp() provides admin' , function ( ) {
59
- const app = firebase . initializeAdminApp ( { databaseName : 'foo' } ) ;
60
- expect ( app . options ) . to . not . have . any . keys ( 'databaseAuthVariableOverride' ) ;
61
- } ) ;
62
-
63
- it ( 'initializeTestApp() throws if no databaseName' , function ( ) {
64
- expect ( firebase . initializeTestApp . bind ( null , { } ) ) . to . throw (
65
- / d a t a b a s e N a m e n o t s p e c i f i e d /
63
+ app = firebase . initializeTestApp ( {
64
+ projectId : 'foo' ,
65
+ auth : { uid : 'alice' }
66
+ } ) ;
67
+ token = await ( app as any ) . INTERNAL . getToken ( ) ;
68
+ expect ( token ) . to . have . any . keys ( 'accessToken' ) ;
69
+ claims = base64 . decodeString (
70
+ token . accessToken . split ( '.' ) [ 1 ] ,
71
+ /*webSafe=*/ false
66
72
) ;
67
- expect (
68
- firebase . initializeTestApp . bind ( null , { databaseName : 'foo' } )
69
- ) . to . not . throw ( ) ;
73
+ expect ( claims ) . to . equal ( '{"uid":"alice"}' ) ;
70
74
} ) ;
71
75
72
- it ( 'initializeTestApp() uses specified auth.' , function ( ) {
73
- let app = firebase . initializeTestApp ( { databaseName : 'foo' } ) ;
74
- expect ( app . options ) . to . have . any . keys ( 'databaseAuthVariableOverride' ) ;
76
+ it ( 'initializeTestApp() with FirestoreAppOptions uses specified auth.' , async function ( ) {
77
+ let app = firebase . initializeTestApp ( {
78
+ projectId : 'foo' ,
79
+ auth : { }
80
+ } ) ;
81
+ let token = await ( app as any ) . INTERNAL . getToken ( ) ;
82
+ expect ( token ) . to . have . any . keys ( 'accessToken' ) ;
83
+ let claims = base64 . decodeString (
84
+ token . accessToken . split ( '.' ) [ 1 ] ,
85
+ /*webSafe=*/ false
86
+ ) ;
87
+ expect ( claims ) . to . equal ( '{}' ) ;
75
88
76
89
app = firebase . initializeTestApp ( {
77
- databaseName : 'foo' ,
90
+ projectId : 'foo' ,
78
91
auth : { uid : 'alice' }
79
92
} ) ;
80
- expect ( app . options ) . to . have . any . keys ( 'databaseAuthVariableOverride' ) ;
81
- expect ( app . options . databaseAuthVariableOverride ) . to . have . all . keys ( 'uid' ) ;
82
- expect ( app . options . databaseAuthVariableOverride [ 'uid' ] ) . to . be . equal (
83
- 'alice'
93
+ token = await ( app as any ) . INTERNAL . getToken ( ) ;
94
+ expect ( token ) . to . have . any . keys ( 'accessToken' ) ;
95
+ claims = base64 . decodeString (
96
+ token . accessToken . split ( '.' ) [ 1 ] ,
97
+ /*webSafe=*/ false
84
98
) ;
99
+ expect ( claims ) . to . equal ( '{"uid":"alice"}' ) ;
85
100
} ) ;
86
101
87
102
it ( 'loadDatabaseRules() throws if no databaseName or rulesPath' , async function ( ) {
@@ -108,13 +123,11 @@ describe('Testing Module Tests', function() {
108
123
) . to . throw ( / C o u l d n o t f i n d f i l e / ) ;
109
124
} ) ;
110
125
111
- it ( 'apps() returns all created apps ' , async function ( ) {
126
+ it ( 'apps() returns apps created with initializeTestApp ' , async function ( ) {
112
127
const numApps = firebase . apps ( ) . length ;
113
- await firebase . initializeAdminApp ( { databaseName : 'foo' } ) ;
128
+ await firebase . initializeTestApp ( { databaseName : 'foo' , auth : { } } ) ;
114
129
expect ( firebase . apps ( ) . length ) . to . equal ( numApps + 1 ) ;
115
- await firebase . initializeAdminApp ( { databaseName : 'foo' } ) ;
130
+ await firebase . initializeTestApp ( { databaseName : 'bar' , auth : { } } ) ;
116
131
expect ( firebase . apps ( ) . length ) . to . equal ( numApps + 2 ) ;
117
- await firebase . initializeTestApp ( { databaseName : 'foo' } ) ;
118
- expect ( firebase . apps ( ) . length ) . to . equal ( numApps + 3 ) ;
119
132
} ) ;
120
133
} ) ;
0 commit comments