-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.service.spec.ts
107 lines (92 loc) · 2.75 KB
/
auth.service.spec.ts
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
import { INestApplication } from "@nestjs/common";
import { Test } from "@nestjs/testing";
import { Server } from "http";
import axios from "axios";
import { AuthService, FirebaseModule } from "../../index";
import { FirebaseEmulatorEnv } from "../../../e2e/firebase-emulator-env";
describe("Firebase Auth Service", () => {
let server: Server;
let app: INestApplication;
let authService: AuthService;
beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [
FirebaseModule.forRoot({
appName: "auth-test",
apiKey: FirebaseEmulatorEnv.apiKey,
projectId: FirebaseEmulatorEnv.projectId,
emulator: {
authUrl: FirebaseEmulatorEnv.authUrl,
},
}),
],
}).compile();
app = module.createNestApplication();
server = app.getHttpServer();
await app.init();
authService = module.get<AuthService>(AuthService);
});
afterAll(async () => {
return await app.close();
});
it(`should fail sign in user`, async () => {
try {
await authService.signInWithEmailAndPassword(
"user.email@gmail.com",
"userpassword",
);
} catch (error) {
expect(error).toMatchInlineSnapshot(
`[FirebaseError: Firebase: Error (auth/user-not-found).]`,
);
}
});
it(`should create and sign in user`, async () => {
const user = (
await authService.createUserWithEmailAndPassword(
"user.email@gmail.com",
"userpassword",
)
).user;
expect(user.email).toMatchInlineSnapshot(`"user.email@gmail.com"`);
const user2 = (
await authService.signInWithEmailAndPassword(
"user.email@gmail.com",
"userpassword",
)
).user;
expect(user2.email).toMatchInlineSnapshot(`"user.email@gmail.com"`);
});
it(`should signInAnonymously`, async () => {
const userCredential = await authService.signInAnonymously();
const toCheck = {
operationType: userCredential.operationType,
user: {
isAnonymous: userCredential.user.isAnonymous,
},
};
expect(toCheck).toMatchInlineSnapshot(`
{
"operationType": "signIn",
"user": {
"isAnonymous": true,
},
}
`);
});
it(`should signOut`, async () => {
await authService.signInAnonymously();
expect((await authService.currentUser()).isAnonymous).toBeTruthy();
await authService.signOut();
expect(await authService.currentUser()).toBeNull();
});
afterEach(async () => {
return await axios
.delete(
"http://localhost:9099/emulator/v1/projects/demo-nhogs-nestjs-firebase/accounts",
)
.then(function (response) {
expect(response.status).toMatchInlineSnapshot(`200`);
});
});
});