Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ test("It caches fetched repository names for user", async () => {
async get() {
return null
},
async set(userId, value) {
async set() {},
async setExpiring(userId: string, value: string) {
cachedUserId = userId
cachedRepositoryNames = value
},
async setExpiring() {},
async delete() {}
},
repositoryAccessReader: {
Expand Down
52 changes: 52 additions & 0 deletions __test__/auth/GuestAccessTokenRepository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { GuestAccessTokenRepository } from "../../src/features/auth/domain"

test("It reads access token for user", async () => {
let readUserId: string | undefined
const sut = new GuestAccessTokenRepository({
async get(userId) {
readUserId = userId
return "foo"
},
async setExpiring() {},
async delete() {}
})
const accessToken = await sut.get("1234")
expect(readUserId).toBe("1234")
expect(accessToken).toBe("foo")
})

test("It stores access token for user", async () => {
let storedUserId: string | undefined
let storedToken: string | undefined
let storedTimeToLive: number | undefined
const sut = new GuestAccessTokenRepository({
async get() {
return "foo"
},
async setExpiring(userId, token, timeToLive) {
storedUserId = userId
storedToken = token
storedTimeToLive = timeToLive
},
async delete(userId) {}
})
await sut.set("1234", "bar")
expect(storedUserId).toBe("1234")
expect(storedToken).toBe("bar")
expect(storedTimeToLive).toBeGreaterThan(0)
})

test("It deletes access token for user", async () => {
let deletedUserId: string | undefined
const sut = new GuestAccessTokenRepository({
async get() {
return "foo"
},
async setExpiring() {},
async delete(userId) {
deletedUserId = userId
}
})
await sut.delete("1234")
expect(deletedUserId).toBe("1234")
})
4 changes: 2 additions & 2 deletions __test__/auth/GuestAccessTokenService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test("It gets the access token for the user", async () => {
readUserId = userId
return "foo"
},
async setExpiring() {}
async set() {}
},
dataSource: {
async getAccessToken() {
Expand All @@ -38,7 +38,7 @@ test("It refreshes access token on demand when there is no cached access token",
async get() {
return null
},
async setExpiring() {}
async set() {}
},
dataSource: {
async getAccessToken() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export interface IRepository {
get(userId: string): Promise<string | null>
setExpiring(userId: string, token: string, timeToLive: number): Promise<void>
delete(userId: string): Promise<void>
}

export default class GuestAccessTokenRepository {
private readonly repository: IRepository

constructor(repository: IRepository) {
this.repository = repository
}

async get(userId: string): Promise<string | null> {
return await this.repository.get(userId)
}

async set(userId: string, accessToken: string): Promise<void> {
await this.repository.setExpiring(userId, accessToken, 7 * 24 * 3600)
}

async delete(userId: string): Promise<void> {
await this.repository.delete(userId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface IUserIDReader {

export interface Repository {
get(userId: string): Promise<string | null>
setExpiring(userId: string, token: string, timeToLive: number): Promise<void>
set(userId: string, token: string): Promise<void>
}

export interface DataSource {
Expand Down Expand Up @@ -47,7 +47,7 @@ export default class GuestAccessTokenService implements IAccessTokenService {
private async getNewAccessToken(): Promise<string> {
const userId = await this.userIdReader.getUserId()
const newAccessToken = await this.dataSource.getAccessToken(userId)
await this.repository.setExpiring(userId, newAccessToken, 7 * 24 * 3600)
await this.repository.set(userId, newAccessToken)
return newAccessToken
}
}
1 change: 1 addition & 0 deletions src/features/auth/domain/accessToken/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as GuestAccessTokenRepository } from "./GuestAccessTokenRepository"
export { default as GuestAccessTokenService } from "./GuestAccessTokenService"
export { default as HostAccessTokenService } from "./HostAccessTokenService"
export { default as LockingAccessTokenService } from "./LockingAccessTokenService"
Expand Down