Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/src/auth-provider-entry.spec.db.ts
2498 views
1
/**
2
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { testContainer } from "./test-container";
8
import { TypeORM } from "./typeorm/typeorm";
9
import { DBAuthProviderEntry } from "./typeorm/entity/db-auth-provider-entry";
10
import { DeepPartial } from "@gitpod/gitpod-protocol/lib/util/deep-partial";
11
import { resetDB } from "./test/reset-db";
12
import { AuthProviderEntryDB } from "./auth-provider-entry-db";
13
import { expect } from "chai";
14
import "mocha";
15
16
const container = testContainer.createChild();
17
18
describe("AuthProviderEntryDBSpec", async () => {
19
let db: AuthProviderEntryDB;
20
21
beforeEach(async () => {
22
db = container.get<AuthProviderEntryDB>(AuthProviderEntryDB);
23
});
24
25
afterEach(async () => {
26
const typeorm = container.get<TypeORM>(TypeORM);
27
await resetDB(typeorm);
28
});
29
30
function authProvider(ap: DeepPartial<DBAuthProviderEntry> = {}): DBAuthProviderEntry {
31
const ownerId = "1234";
32
const host = "github.com";
33
return {
34
id: "0049b9d2-005f-43c2-a0ae-76377805d8b8",
35
host,
36
ownerId,
37
organizationId: null!,
38
status: "verified",
39
type: "GitHub",
40
oauthRevision: undefined,
41
...ap,
42
oauth: {
43
callBackUrl: "example.org/some/callback",
44
authorizationUrl: "example.org/some/auth",
45
settingsUrl: "example.org/settings",
46
clientId: "clientId",
47
clientSecret: "clientSecret",
48
tokenUrl: "example.org/get/token",
49
scope: "scope",
50
scopeSeparator: ",",
51
...ap.oauth,
52
authorizationParams: {},
53
},
54
};
55
}
56
57
it("should findAll", async () => {
58
const ap1 = authProvider({ id: "1", oauthRevision: "rev1" });
59
const ap2 = authProvider({ id: "2", oauthRevision: "rev2" });
60
await db.storeAuthProvider(ap1, false);
61
await db.storeAuthProvider(ap2, false);
62
63
const all = await db.findAll();
64
expect(all, "findAll([])").to.deep.equal([ap1, ap2]);
65
expect(await db.findAll([ap1.oauthRevision!, ap2.oauthRevision!]), "findAll([ap1, ap2])").to.be.empty;
66
expect(await db.findAll([ap1.oauthRevision!]), "findAll([ap1])").to.deep.equal([ap2]);
67
}).timeout(30000); // this test is sometimes slow because it is the first one and ts-node needs to compile
68
69
it("should findAllHosts", async () => {
70
const ap1 = authProvider({ id: "1", oauthRevision: "rev1", host: "foo" });
71
const ap2 = authProvider({ id: "2", oauthRevision: "rev2", host: "BAR" });
72
await db.storeAuthProvider(ap1, false);
73
await db.storeAuthProvider(ap2, false);
74
75
const all = await db.findAllHosts();
76
expect(all.sort(), "findAllHosts([])").to.deep.equal(["foo", "bar"].sort());
77
});
78
79
it("should oauthRevision", async () => {
80
const ap = authProvider({ id: "1" });
81
await db.storeAuthProvider(ap, true);
82
83
const loadedAp = await db.findByHost(ap.host);
84
expect(loadedAp, "findByHost()").to.deep.equal(ap);
85
expect(loadedAp?.oauthRevision, "findByHost()").to.equal(
86
"3d1390670fd19c27157d046960c3d7c46df81db642302dea1a9fe86cf0594361",
87
);
88
});
89
90
it("should findByOrgId()", async () => {
91
const ap1 = authProvider({ id: "1", organizationId: "O1", host: "H1" });
92
const ap2 = authProvider({ id: "2", organizationId: "O1", host: "H2" });
93
const ap3 = authProvider({ id: "3", organizationId: "O2", host: "H1" });
94
95
await db.storeAuthProvider(ap1, false);
96
await db.storeAuthProvider(ap2, false);
97
await db.storeAuthProvider(ap3, false);
98
99
const results = await db.findByOrgId("O1");
100
expect(results.length).to.equal(2);
101
expect(results).to.deep.contain(ap1);
102
expect(results).to.deep.contain(ap2);
103
});
104
105
it("should findByUserId", async () => {
106
const ap1 = authProvider({ id: "1", ownerId: "owner1" });
107
const ap2 = authProvider({ id: "2", ownerId: "owner1", organizationId: "org1" });
108
109
await db.storeAuthProvider(ap1, false);
110
await db.storeAuthProvider(ap2, false);
111
112
const results = await db.findByUserId("owner1");
113
expect(results.length).to.equal(1);
114
expect(results).to.deep.contain(ap1);
115
});
116
});
117
118