Skip to content

Commit

Permalink
test(analytics): adding a best-effort verification of auth token vali…
Browse files Browse the repository at this point in the history
…dity + minor cleanup
  • Loading branch information
mkhq committed Sep 6, 2023
1 parent 99a5122 commit 632004c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 32 deletions.
5 changes: 1 addition & 4 deletions core/src/analytics/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ export class AnalyticsHandler {
isEnabled,
ciInfo,
projectName,
fallbackCloudDomain,
}: {
garden: Garden
log: Log
Expand All @@ -294,7 +293,6 @@ export class AnalyticsHandler {
cloudUser?: CloudUserProfile
ciInfo: CiInfo
projectName: string
fallbackCloudDomain?: string
}) {
const segmentClient = require("analytics-node")
const segmentApiKey = gardenEnv.ANALYTICS_DEV ? SEGMENT_DEV_API_KEY : SEGMENT_PROD_API_KEY
Expand Down Expand Up @@ -372,7 +370,7 @@ export class AnalyticsHandler {
if (cloudUser) {
this.cloudUserId = AnalyticsHandler.makeUniqueCloudUserId(cloudUser)
this.cloudOrganizationName = cloudUser.organizationName
this.cloudDomain = cloudUser.domain
this.isLoggedIn = true
}

this.isRecurringUser = getIsRecurringUser(analyticsConfig.firstRunAt, analyticsConfig.latestRunAt)
Expand Down Expand Up @@ -535,7 +533,6 @@ export class AnalyticsHandler {
ciInfo,
anonymousUserId,
projectName,
fallbackCloudDomain,
})
}

Expand Down
4 changes: 4 additions & 0 deletions core/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ export class CloudApi {
return undefined
}

if (authToken.validity < new Date()) {
return undefined
}

if (!authToken.userId || !authToken.organizationName) {
return undefined
}
Expand Down
56 changes: 28 additions & 28 deletions core/test/unit/src/cli/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,6 @@ describe("cli analytics", () => {
}
}

describe("version check service", () => {
beforeEach(async () => {
// the version check service is mocked here so its safe to enable the check in tests
gardenEnv.GARDEN_DISABLE_VERSION_CHECK = false
})

afterEach(async () => {
gardenEnv.GARDEN_DISABLE_VERSION_CHECK = true
})

it("should access the version check service", async () => {
const scope = nock("https://get.garden.io")
scope.get("/version").query(true).reply(200)

const command = new TestCommand()
cli.addCommand(command)

await cli.run({ args: ["test-command"], exitOnError: false, cwd: garden.projectRoot })

expect(scope.done()).to.not.throw
})
})

it("should wait for queued analytic events to flush", async () => {
const scope = nock("https://api.segment.io")

Expand Down Expand Up @@ -173,7 +150,7 @@ describe("cli analytics", () => {
projectName: event.properties.projectNameV2,
}))

expect(events).to.eql([
return isEqual(events, [
{
event: "Run Command",
type: "track",
Expand All @@ -187,7 +164,6 @@ describe("cli analytics", () => {
projectName: AnalyticsHandler.hashV2("test-project-a"),
},
])
return true
})
.reply(200)

Expand Down Expand Up @@ -246,26 +222,27 @@ describe("cli analytics", () => {
name: event.properties.name,
cloudUserId: event.properties.cloudUserId,
organizationName: event.properties.organizationName,
isLoggedIn: event.properties.isLoggedIn,
}))

expect(events).to.eql([
return isEqual(events, [
{
event: "Run Command",
type: "track",
name: "test-command",
cloudUserId: uniqueCloudUserId,
organizationName: cloudOrganizationName,
isLoggedIn: true,
},
{
event: "Command Result",
type: "track",
name: "test-command",
cloudUserId: uniqueCloudUserId,
organizationName: cloudOrganizationName,
isLoggedIn: true,
},
])

return true
})
.reply(200)

Expand All @@ -277,4 +254,27 @@ describe("cli analytics", () => {
expect(scope.done()).to.not.throw
})
})

describe("version check service", () => {
beforeEach(async () => {
// the version check service is mocked here so its safe to enable the check in tests
gardenEnv.GARDEN_DISABLE_VERSION_CHECK = false
})

afterEach(async () => {
gardenEnv.GARDEN_DISABLE_VERSION_CHECK = true
})

it("should access the version check service", async () => {
const scope = nock("https://get.garden.io")
scope.get("/version").query(true).reply(200)

const command = new TestCommand()
cli.addCommand(command)

await cli.run({ args: ["test-command"], exitOnError: false, cwd: garden.projectRoot })

expect(scope.done()).to.not.throw
})
})
})
20 changes: 20 additions & 0 deletions core/test/unit/src/cloud/auth-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ describe("AuthToken", () => {
expect(savedProfile).to.eql(userProfile)
})

it("should not return a user profile when the token has expired", async () => {
const testToken = {
token: uuidv4(),
refreshToken: uuidv4(),
tokenValidity: -9999,
}
const userProfile: CloudUserProfile = {
userId: "some-uuid",
organizationName: "some-org-name",
domain,
}

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

const savedProfile = await CloudApi.getAuthTokenUserProfile(log, globalConfigStore, domain)
expect(savedProfile).to.be.undefined
})

it("should return the value of GARDEN_AUTH_TOKEN if it's present", async () => {
const tokenBackup = gardenEnv.GARDEN_AUTH_TOKEN
const testToken = "token-from-env"
Expand Down

0 comments on commit 632004c

Please sign in to comment.