Path: blob/main/components/gitpod-db/src/auth-provider-entry.spec.db.ts
2498 views
/**1* Copyright (c) 2022 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { testContainer } from "./test-container";7import { TypeORM } from "./typeorm/typeorm";8import { DBAuthProviderEntry } from "./typeorm/entity/db-auth-provider-entry";9import { DeepPartial } from "@gitpod/gitpod-protocol/lib/util/deep-partial";10import { resetDB } from "./test/reset-db";11import { AuthProviderEntryDB } from "./auth-provider-entry-db";12import { expect } from "chai";13import "mocha";1415const container = testContainer.createChild();1617describe("AuthProviderEntryDBSpec", async () => {18let db: AuthProviderEntryDB;1920beforeEach(async () => {21db = container.get<AuthProviderEntryDB>(AuthProviderEntryDB);22});2324afterEach(async () => {25const typeorm = container.get<TypeORM>(TypeORM);26await resetDB(typeorm);27});2829function authProvider(ap: DeepPartial<DBAuthProviderEntry> = {}): DBAuthProviderEntry {30const ownerId = "1234";31const host = "github.com";32return {33id: "0049b9d2-005f-43c2-a0ae-76377805d8b8",34host,35ownerId,36organizationId: null!,37status: "verified",38type: "GitHub",39oauthRevision: undefined,40...ap,41oauth: {42callBackUrl: "example.org/some/callback",43authorizationUrl: "example.org/some/auth",44settingsUrl: "example.org/settings",45clientId: "clientId",46clientSecret: "clientSecret",47tokenUrl: "example.org/get/token",48scope: "scope",49scopeSeparator: ",",50...ap.oauth,51authorizationParams: {},52},53};54}5556it("should findAll", async () => {57const ap1 = authProvider({ id: "1", oauthRevision: "rev1" });58const ap2 = authProvider({ id: "2", oauthRevision: "rev2" });59await db.storeAuthProvider(ap1, false);60await db.storeAuthProvider(ap2, false);6162const all = await db.findAll();63expect(all, "findAll([])").to.deep.equal([ap1, ap2]);64expect(await db.findAll([ap1.oauthRevision!, ap2.oauthRevision!]), "findAll([ap1, ap2])").to.be.empty;65expect(await db.findAll([ap1.oauthRevision!]), "findAll([ap1])").to.deep.equal([ap2]);66}).timeout(30000); // this test is sometimes slow because it is the first one and ts-node needs to compile6768it("should findAllHosts", async () => {69const ap1 = authProvider({ id: "1", oauthRevision: "rev1", host: "foo" });70const ap2 = authProvider({ id: "2", oauthRevision: "rev2", host: "BAR" });71await db.storeAuthProvider(ap1, false);72await db.storeAuthProvider(ap2, false);7374const all = await db.findAllHosts();75expect(all.sort(), "findAllHosts([])").to.deep.equal(["foo", "bar"].sort());76});7778it("should oauthRevision", async () => {79const ap = authProvider({ id: "1" });80await db.storeAuthProvider(ap, true);8182const loadedAp = await db.findByHost(ap.host);83expect(loadedAp, "findByHost()").to.deep.equal(ap);84expect(loadedAp?.oauthRevision, "findByHost()").to.equal(85"3d1390670fd19c27157d046960c3d7c46df81db642302dea1a9fe86cf0594361",86);87});8889it("should findByOrgId()", async () => {90const ap1 = authProvider({ id: "1", organizationId: "O1", host: "H1" });91const ap2 = authProvider({ id: "2", organizationId: "O1", host: "H2" });92const ap3 = authProvider({ id: "3", organizationId: "O2", host: "H1" });9394await db.storeAuthProvider(ap1, false);95await db.storeAuthProvider(ap2, false);96await db.storeAuthProvider(ap3, false);9798const results = await db.findByOrgId("O1");99expect(results.length).to.equal(2);100expect(results).to.deep.contain(ap1);101expect(results).to.deep.contain(ap2);102});103104it("should findByUserId", async () => {105const ap1 = authProvider({ id: "1", ownerId: "owner1" });106const ap2 = authProvider({ id: "2", ownerId: "owner1", organizationId: "org1" });107108await db.storeAuthProvider(ap1, false);109await db.storeAuthProvider(ap2, false);110111const results = await db.findByUserId("owner1");112expect(results.length).to.equal(1);113expect(results).to.deep.contain(ap1);114});115});116117118