From 632004c57e9e9cec443e095b2631049d00da8b91 Mon Sep 17 00:00:00 2001 From: Mikael Hoegqvist Tabor Date: Wed, 30 Aug 2023 14:40:20 +0200 Subject: [PATCH] test(analytics): adding a best-effort verification of auth token validity + minor cleanup --- core/src/analytics/analytics.ts | 5 +-- core/src/cloud/api.ts | 4 ++ core/test/unit/src/cli/analytics.ts | 56 +++++++++++++------------- core/test/unit/src/cloud/auth-token.ts | 20 +++++++++ 4 files changed, 53 insertions(+), 32 deletions(-) diff --git a/core/src/analytics/analytics.ts b/core/src/analytics/analytics.ts index f559cf6459..fe4fee2685 100644 --- a/core/src/analytics/analytics.ts +++ b/core/src/analytics/analytics.ts @@ -282,7 +282,6 @@ export class AnalyticsHandler { isEnabled, ciInfo, projectName, - fallbackCloudDomain, }: { garden: Garden log: Log @@ -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 @@ -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) @@ -535,7 +533,6 @@ export class AnalyticsHandler { ciInfo, anonymousUserId, projectName, - fallbackCloudDomain, }) } diff --git a/core/src/cloud/api.ts b/core/src/cloud/api.ts index f8d6ae1537..a41d8077d3 100644 --- a/core/src/cloud/api.ts +++ b/core/src/cloud/api.ts @@ -340,6 +340,10 @@ export class CloudApi { return undefined } + if (authToken.validity < new Date()) { + return undefined + } + if (!authToken.userId || !authToken.organizationName) { return undefined } diff --git a/core/test/unit/src/cli/analytics.ts b/core/test/unit/src/cli/analytics.ts index 90ec9684d0..fd73d35157 100644 --- a/core/test/unit/src/cli/analytics.ts +++ b/core/test/unit/src/cli/analytics.ts @@ -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") @@ -173,7 +150,7 @@ describe("cli analytics", () => { projectName: event.properties.projectNameV2, })) - expect(events).to.eql([ + return isEqual(events, [ { event: "Run Command", type: "track", @@ -187,7 +164,6 @@ describe("cli analytics", () => { projectName: AnalyticsHandler.hashV2("test-project-a"), }, ]) - return true }) .reply(200) @@ -246,15 +222,17 @@ 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", @@ -262,10 +240,9 @@ describe("cli analytics", () => { name: "test-command", cloudUserId: uniqueCloudUserId, organizationName: cloudOrganizationName, + isLoggedIn: true, }, ]) - - return true }) .reply(200) @@ -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 + }) + }) }) diff --git a/core/test/unit/src/cloud/auth-token.ts b/core/test/unit/src/cloud/auth-token.ts index 1a2272dcb5..4cab50265a 100644 --- a/core/test/unit/src/cloud/auth-token.ts +++ b/core/test/unit/src/cloud/auth-token.ts @@ -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"