Skip to content

Commit

Permalink
chore(cloud): refactored save token api to use named params
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhq committed Oct 25, 2023
1 parent ada15d8 commit ed4145c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 23 deletions.
30 changes: 22 additions & 8 deletions core/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ export interface CloudApiFactoryParams {
skipLogging?: boolean
}

export interface SaveAuthTokenParams {
log: Log
globalConfigStore: GlobalConfigStore
tokenResponse: AuthTokenResponse
domain: string
userProfile?: CloudUserProfile
}

export type CloudApiFactory = (params: CloudApiFactoryParams) => Promise<CloudApi | undefined>

/**
Expand Down Expand Up @@ -260,13 +268,13 @@ export class CloudApi {
return api
}

static async saveAuthToken(
log: Log,
globalConfigStore: GlobalConfigStore,
tokenResponse: AuthTokenResponse,
domain: string,
userProfile: CloudUserProfile | undefined = undefined
) {
static async saveAuthToken({
log,
globalConfigStore,
tokenResponse,
domain,
userProfile = undefined,
}: SaveAuthTokenParams) {
const distroName = getCloudDistributionName(domain)

if (!tokenResponse.token) {
Expand Down Expand Up @@ -504,7 +512,13 @@ export class CloudApi {
domain: this.domain,
}
}
await CloudApi.saveAuthToken(this.log, this.globalConfigStore, tokenObj, this.domain, userProfile)
await CloudApi.saveAuthToken({
log: this.log,
globalConfigStore: this.globalConfigStore,
tokenResponse: tokenObj,
domain: this.domain,
userProfile,
})
} catch (err) {
if (!(err instanceof GotHttpError)) {
throw err
Expand Down
4 changes: 2 additions & 2 deletions core/src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class LoginCommand extends Command<{}, Opts> {
log.info({ msg: `Logging in to ${cloudDomain}...` })
const tokenResponse = await login(log, cloudDomain, garden.events)
// Save the token, then try to create a cloud API instance and retrieve the profile
await CloudApi.saveAuthToken(log, globalConfigStore, tokenResponse, cloudDomain)
await CloudApi.saveAuthToken({ log, globalConfigStore, tokenResponse, domain: cloudDomain })

try {
cloudApi = await CloudApi.factory({ log, cloudDomain, skipLogging: true, globalConfigStore })
Expand All @@ -137,7 +137,7 @@ export class LoginCommand extends Command<{}, Opts> {
log.silly(`Failed to retreive the user profile after retrieving access token, ${err}`)
}

await CloudApi.saveAuthToken(log, globalConfigStore, tokenResponse, cloudDomain, userProfile)
await CloudApi.saveAuthToken({ log, globalConfigStore, tokenResponse, domain: cloudDomain, userProfile })
log.info({ msg: `Successfully logged in to ${cloudDomain}.` })
} catch (err) {
await CloudApi.clearAuthToken(log, globalConfigStore, cloudDomain)
Expand Down
8 changes: 7 additions & 1 deletion core/test/unit/src/cli/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ describe("cli analytics", () => {
domain,
}

await CloudApi.saveAuthToken(log, garden.globalConfigStore, testToken, domain, userProfile)
await CloudApi.saveAuthToken({
log,
globalConfigStore: garden.globalConfigStore,
tokenResponse: testToken,
domain,
userProfile,
})
})

afterEach(async () => {
Expand Down
8 changes: 4 additions & 4 deletions core/test/unit/src/cloud/auth-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("AuthToken", () => {
refreshToken: uuidv4(),
tokenValidity: 9999,
}
await CloudApi.saveAuthToken(log, globalConfigStore, testToken, domain)
await CloudApi.saveAuthToken({ log, globalConfigStore, tokenResponse: testToken, domain })
const savedToken = await CloudApi.getAuthToken(log, globalConfigStore, domain)
expect(savedToken).to.eql(testToken.token)
})
Expand All @@ -60,7 +60,7 @@ describe("AuthToken", () => {
domain,
}

await CloudApi.saveAuthToken(log, globalConfigStore, testToken, domain, userProfile)
await CloudApi.saveAuthToken({ log, globalConfigStore, tokenResponse: testToken, domain, userProfile })
const savedToken = await CloudApi.getAuthToken(log, globalConfigStore, domain)
expect(savedToken).to.eql(testToken.token)

Expand All @@ -80,7 +80,7 @@ describe("AuthToken", () => {
domain,
}

await CloudApi.saveAuthToken(log, globalConfigStore, testToken, domain, userProfile)
await CloudApi.saveAuthToken({ log, globalConfigStore, tokenResponse: testToken, domain, userProfile })
const savedToken = await CloudApi.getAuthToken(log, globalConfigStore, domain)
expect(savedToken).to.eql(testToken.token)

Expand Down Expand Up @@ -108,7 +108,7 @@ describe("AuthToken", () => {
refreshToken: uuidv4(),
tokenValidity: 9999,
}
await CloudApi.saveAuthToken(log, globalConfigStore, testToken, domain)
await CloudApi.saveAuthToken({ log, globalConfigStore, tokenResponse: testToken, domain })
await CloudApi.clearAuthToken(log, globalConfigStore, domain)
const savedToken = await CloudApi.getAuthToken(log, globalConfigStore, domain)
expect(savedToken).to.be.undefined
Expand Down
21 changes: 18 additions & 3 deletions core/test/unit/src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ describe("LoginCommand", () => {
globalConfigStore,
})

await CloudApi.saveAuthToken(garden.log, garden.globalConfigStore, testToken, garden.cloudDomain!)
await CloudApi.saveAuthToken({
log: garden.log,
globalConfigStore: garden.globalConfigStore,
tokenResponse: testToken,
domain: garden.cloudDomain!,
})
td.replace(CloudApi.prototype, "checkClientAuthToken", async () => true)
td.replace(CloudApi.prototype, "startInterval", async () => {})

Expand Down Expand Up @@ -282,7 +287,12 @@ describe("LoginCommand", () => {
globalConfigStore,
})

await CloudApi.saveAuthToken(garden.log, garden.globalConfigStore, testToken, garden.cloudDomain!)
await CloudApi.saveAuthToken({
log: garden.log,
globalConfigStore: garden.globalConfigStore,
tokenResponse: testToken,
domain: garden.cloudDomain!,
})
td.replace(CloudApi.prototype, "checkClientAuthToken", async () => false)
td.replace(CloudApi.prototype, "refreshToken", async () => {
throw new Error("bummer")
Expand Down Expand Up @@ -313,7 +323,12 @@ describe("LoginCommand", () => {
globalConfigStore,
})

await CloudApi.saveAuthToken(garden.log, garden.globalConfigStore, testToken, garden.cloudDomain!)
await CloudApi.saveAuthToken({
log: garden.log,
globalConfigStore: garden.globalConfigStore,
tokenResponse: testToken,
domain: garden.cloudDomain!,
})
td.replace(CloudApi.prototype, "checkClientAuthToken", async () => false)
td.replace(CloudApi.prototype, "refreshToken", async () => {
throw new CloudApiError({ message: "bummer", responseStatusCode: 401 })
Expand Down
35 changes: 30 additions & 5 deletions core/test/unit/src/commands/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ describe("LogoutCommand", () => {
globalConfigStore,
})

await CloudApi.saveAuthToken(garden.log, garden.globalConfigStore, testToken, garden.cloudDomain!)
await CloudApi.saveAuthToken({
log: garden.log,
globalConfigStore: garden.globalConfigStore,
tokenResponse: testToken,
domain: garden.cloudDomain!,
})
td.replace(CloudApi.prototype, "checkClientAuthToken", async () => true)
td.replace(CloudApi.prototype, "startInterval", async () => {})
td.replace(CloudApi.prototype, "post", async () => {})
Expand Down Expand Up @@ -98,7 +103,12 @@ describe("LogoutCommand", () => {
commandInfo: { name: "foo", args: {}, opts: {} },
})

await CloudApi.saveAuthToken(garden.log, garden.globalConfigStore, testToken, garden.cloudDomain!)
await CloudApi.saveAuthToken({
log: garden.log,
globalConfigStore: garden.globalConfigStore,
tokenResponse: testToken,
domain: garden.cloudDomain!,
})
td.replace(CloudApi.prototype, "checkClientAuthToken", async () => true)
td.replace(CloudApi.prototype, "startInterval", async () => {})
td.replace(CloudApi.prototype, "post", async () => {})
Expand Down Expand Up @@ -151,7 +161,12 @@ describe("LogoutCommand", () => {
globalConfigStore,
})

await CloudApi.saveAuthToken(garden.log, garden.globalConfigStore, testToken, garden.cloudDomain!)
await CloudApi.saveAuthToken({
log: garden.log,
globalConfigStore: garden.globalConfigStore,
tokenResponse: testToken,
domain: garden.cloudDomain!,
})
// Throw when initializing Enterprise API
td.replace(CloudApi.prototype, "factory", async () => {
throw new Error("Not tonight")
Expand Down Expand Up @@ -191,7 +206,12 @@ describe("LogoutCommand", () => {
globalConfigStore,
})

await CloudApi.saveAuthToken(garden.log, garden.globalConfigStore, testToken, garden.cloudDomain!)
await CloudApi.saveAuthToken({
log: garden.log,
globalConfigStore: garden.globalConfigStore,
tokenResponse: testToken,
domain: garden.cloudDomain!,
})
// Throw when using Enterprise API to call logout endpoint
td.replace(CloudApi.prototype, "post", async () => {
throw new Error("Not tonight")
Expand Down Expand Up @@ -249,7 +269,12 @@ describe("LogoutCommand", () => {
globalConfigStore,
})

await CloudApi.saveAuthToken(garden.log, garden.globalConfigStore, testToken, garden.cloudDomain!)
await CloudApi.saveAuthToken({
log: garden.log,
globalConfigStore: garden.globalConfigStore,
tokenResponse: testToken,
domain: garden.cloudDomain!,
})
td.replace(CloudApi.prototype, "checkClientAuthToken", async () => true)
td.replace(CloudApi.prototype, "startInterval", async () => {})
td.replace(CloudApi.prototype, "post", async () => {})
Expand Down

0 comments on commit ed4145c

Please sign in to comment.