-
Notifications
You must be signed in to change notification settings - Fork 1
/
authentication-service.test.ts
94 lines (92 loc) · 3.58 KB
/
authentication-service.test.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
import { describe } from "node:test";
import { AuthenticationService } from "./authentication-service";
import { UserAccountRepositoryInMemory } from "./memory/user-account-repoistory-in-memory";
import { UserAccountRepository } from "./user-account-repository";
import { SessionRepositoryInMemory } from "./memory/session-repository-in-memory";
import { SessionRepository } from "./session-repository";
import { UserAccount } from "../domain/user-account";
import { UserAccountId } from "../domain/user-account-id";
afterEach(() => {
jest.useRealTimers();
});
describe("AuthenticationService", () => {
test("登録済みの未ログインユーザがログインできる", async () => {
// Given
const userAccountRepository: UserAccountRepository =
UserAccountRepositoryInMemory.create();
const sessionRepository: SessionRepository =
SessionRepositoryInMemory.create();
const authenticationService: AuthenticationService =
AuthenticationService.create(userAccountRepository, sessionRepository);
const userAccount = UserAccount.of(
UserAccountId.of("taro@gmail.com"),
"Yamada",
"Taro",
"password",
);
await userAccountRepository.save(userAccount);
// When
const [s, u] = await authenticationService.login(
userAccount.id.value,
userAccount.password,
);
// Then
expect(u).toBe(userAccount);
expect(s.userAccountId).toBe(userAccount.id);
});
test("登録されていないユーザはログインできない", async () => {
// Given
const userAccountRepository: UserAccountRepository = UserAccountRepositoryInMemory.create();
const sessionRepository: SessionRepository = SessionRepositoryInMemory.create();
const authenticationService: AuthenticationService =
AuthenticationService.create(userAccountRepository, sessionRepository);
// When
// Then
await expect(
authenticationService.login("taro@gmail.com", "password"),
).rejects.toThrow("User not found");
});
test("登録済みユーザがパスワードを間違うとログインできない", async () => {
const userAccountRepository: UserAccountRepository = UserAccountRepositoryInMemory.create();
const sessionRepository: SessionRepository = SessionRepositoryInMemory.create();
const authenticationService: AuthenticationService =
AuthenticationService.create(userAccountRepository, sessionRepository);
const userAccount = UserAccount.of(
UserAccountId.of("taro@gmail.com"),
"Yamada",
"Taro",
"password",
);
await userAccountRepository.save(userAccount);
// When
// Then
await expect(authenticationService.login(
userAccount.id.value,
"wrong password",
)).rejects.toThrow("Invalid credentials");
})
test("登録済みのログイン済みユーザがログアウトできる", async () => {
// Given
const userAccountRepository: UserAccountRepository =
UserAccountRepositoryInMemory.create();
const sessionRepository: SessionRepository =
SessionRepositoryInMemory.create();
const authenticationService: AuthenticationService =
AuthenticationService.create(userAccountRepository, sessionRepository);
const userAccount = UserAccount.of(
UserAccountId.of("taro@gmail.com"),
"Yamada",
"Taro",
"password",
);
await userAccountRepository.save(userAccount);
const [s] = await authenticationService.login(
userAccount.id.value,
userAccount.password,
);
// When
const result = await authenticationService.logout(s.id);
// Then
expect(result).toBeTruthy()
})
});