Skip to content

Commit

Permalink
Merge branch 'develop' into fix/image-cache-header
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbs authored Nov 2, 2023
2 parents 9c7a491 + 8b357e1 commit b717637
Show file tree
Hide file tree
Showing 30 changed files with 688 additions and 499 deletions.
107 changes: 0 additions & 107 deletions __test__/auth/AccessTokenService.test.ts

This file was deleted.

52 changes: 9 additions & 43 deletions __test__/auth/InitialOAuthTokenService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,13 @@ test("It fetches refresh token for specified user", async () => {
}
},
oAuthTokenRefresher: {
async refreshAccessToken() {
return {
accessToken: "foo",
refreshToken: "bar",
accessTokenExpiryDate: new Date(),
refreshTokenExpiryDate: new Date()
}
async refreshOAuthToken() {
return { accessToken: "foo", refreshToken: "bar" }
}
},
oAuthTokenRepository: {
async getOAuthToken() {
return {
accessToken: "foo",
refreshToken: "bar",
accessTokenExpiryDate: new Date(),
refreshTokenExpiryDate: new Date()
}
return { accessToken: "foo", refreshToken: "bar" }
},
async storeOAuthToken() {},
async deleteOAuthToken() {}
Expand All @@ -46,24 +36,14 @@ test("It refreshes the fetched refresh token", async () => {
}
},
oAuthTokenRefresher: {
async refreshAccessToken(refreshToken) {
async refreshOAuthToken(refreshToken) {
refreshedRefreshToken = refreshToken
return {
accessToken: "foo",
refreshToken: "bar",
accessTokenExpiryDate: new Date(),
refreshTokenExpiryDate: new Date()
}
return { accessToken: "foo", refreshToken: "bar" }
}
},
oAuthTokenRepository: {
async getOAuthToken() {
return {
accessToken: "foo",
refreshToken: "bar",
accessTokenExpiryDate: new Date(),
refreshTokenExpiryDate: new Date()
}
return { accessToken: "foo", refreshToken: "bar" }
},
async storeOAuthToken() {},
async deleteOAuthToken() {}
Expand All @@ -76,32 +56,20 @@ test("It refreshes the fetched refresh token", async () => {
test("It stores the refreshed auth token for the correct user ID", async () => {
let storedAuthToken: OAuthToken | undefined
let storedUserId: string | undefined
const accessTokenExpiryDate = new Date(new Date().getTime() + 3600 * 1000)
const refreshTokenExpiryDate = new Date(new Date().getTime() + 24 * 3600 * 1000)
const sut = new InitialOAuthTokenService({
refreshTokenReader: {
async getRefreshToken() {
return "helloworld"
}
},
oAuthTokenRefresher: {
async refreshAccessToken() {
return {
accessToken: "foo",
refreshToken: "bar",
accessTokenExpiryDate: accessTokenExpiryDate,
refreshTokenExpiryDate: refreshTokenExpiryDate
}
async refreshOAuthToken() {
return { accessToken: "foo", refreshToken: "bar" }
}
},
oAuthTokenRepository: {
async getOAuthToken() {
return {
accessToken: "foo",
refreshToken: "bar",
accessTokenExpiryDate: new Date(),
refreshTokenExpiryDate: new Date()
}
return { accessToken: "foo", refreshToken: "bar" }
},
async storeOAuthToken(userId, token) {
storedAuthToken = token
Expand All @@ -113,7 +81,5 @@ test("It stores the refreshed auth token for the correct user ID", async () => {
await sut.fetchInitialAuthTokenForUser("123")
expect(storedAuthToken?.accessToken).toBe("foo")
expect(storedAuthToken?.refreshToken).toBe("bar")
expect(storedAuthToken?.accessTokenExpiryDate).toBe(accessTokenExpiryDate)
expect(storedAuthToken?.refreshTokenExpiryDate).toBe(refreshTokenExpiryDate)
expect(storedUserId).toBe("123")
})
131 changes: 131 additions & 0 deletions __test__/auth/LockingOAuthTokenRefresher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import LockingAccessTokenRefresher from "../../src/features/auth/domain/LockingAccessTokenRefresher"
import OAuthToken from "../../src/features/auth/domain/OAuthToken"

test("It acquires a lock", async () => {
let didAcquireLock = false
const sut = new LockingAccessTokenRefresher({
async makeMutex() {
return {
async acquire() {
didAcquireLock = true
},
async release() {}
}
}
}, {
async getOAuthToken() {
return { accessToken: "foo", refreshToken: "bar" }
},
async storeOAuthToken() {},
async deleteOAuthToken() {}
}, {
async refreshOAuthToken() {
return { accessToken: "foo", refreshToken: "bar" }
}
})
await sut.refreshAccessToken("bar")
expect(didAcquireLock).toBeTruthy()
})

test("It releases the acquired lock", async () => {
let didReleaseLock = false
const sut = new LockingAccessTokenRefresher({
async makeMutex() {
return {
async acquire() {},
async release() {
didReleaseLock = true
}
}
}
}, {
async getOAuthToken() {
return { accessToken: "foo", refreshToken: "bar" }
},
async storeOAuthToken() {},
async deleteOAuthToken() {}
}, {
async refreshOAuthToken() {
return { accessToken: "foo", refreshToken: "bar" }
}
})
await sut.refreshAccessToken("bar")
expect(didReleaseLock).toBeTruthy()
})

test("It refreshes the access token when the input access token matches the stored access token", async () => {
let didRefreshAccessToken = false
const sut = new LockingAccessTokenRefresher({
async makeMutex() {
return {
async acquire() {},
async release() {}
}
}
}, {
async getOAuthToken() {
return { accessToken: "foo", refreshToken: "bar" }
},
async storeOAuthToken() {},
async deleteOAuthToken() {}
}, {
async refreshOAuthToken() {
didRefreshAccessToken = true
return { accessToken: "foo", refreshToken: "bar" }
}
})
await sut.refreshAccessToken("foo")
expect(didRefreshAccessToken).toBeTruthy()
})

test("It skips refreshing the access token when the input access token is not equal to the stored access token", async () => {
let didRefreshAccessToken = false
const sut = new LockingAccessTokenRefresher({
async makeMutex() {
return {
async acquire() {},
async release() {}
}
}
}, {
async getOAuthToken() {
return { accessToken: "new", refreshToken: "bar" }
},
async storeOAuthToken() {},
async deleteOAuthToken() {}
}, {
async refreshOAuthToken() {
didRefreshAccessToken = true
return { accessToken: "foo", refreshToken: "bar" }
}
})
await sut.refreshAccessToken("outdated")
expect(didRefreshAccessToken).toBeFalsy()
})

test("It stores the refreshed tokens", async () => {
let storedToken: OAuthToken | undefined
const sut = new LockingAccessTokenRefresher({
async makeMutex() {
return {
async acquire() {},
async release() {}
}
}
}, {
async getOAuthToken() {
return { accessToken: "foo", refreshToken: "bar" }
},
async storeOAuthToken(token) {
storedToken = token
},
async deleteOAuthToken() {}
}, {
async refreshOAuthToken() {
return { accessToken: "newAccessToken", refreshToken: "newRefreshToken" }
}
})
await sut.refreshAccessToken("foo")
expect(storedToken?.accessToken).toEqual("newAccessToken")
expect(storedToken?.refreshToken).toEqual("newRefreshToken")
})
49 changes: 0 additions & 49 deletions __test__/auth/SessionLockingAccessTokenService.test.ts

This file was deleted.

Loading

0 comments on commit b717637

Please sign in to comment.