|
| 1 | +// @flow |
| 2 | +import TestServer from "fetch-test-server"; |
| 3 | +import uuid from "uuid"; |
| 4 | +import app from "../app"; |
| 5 | +import { buildUser, buildAdmin, buildTeam } from "../test/factories"; |
| 6 | +import { flushdb } from "../test/support"; |
| 7 | + |
| 8 | +const server = new TestServer(app.callback()); |
| 9 | + |
| 10 | +beforeEach(() => flushdb()); |
| 11 | +afterAll(() => server.close()); |
| 12 | + |
| 13 | +describe("#authenticationProviders.info", () => { |
| 14 | + it("should return auth provider", async () => { |
| 15 | + const team = await buildTeam(); |
| 16 | + const user = await buildUser({ teamId: team.id }); |
| 17 | + const authenticationProviders = await team.getAuthenticationProviders(); |
| 18 | + |
| 19 | + const res = await server.post("/api/authenticationProviders.info", { |
| 20 | + body: { |
| 21 | + id: authenticationProviders[0].id, |
| 22 | + token: user.getJwtToken(), |
| 23 | + }, |
| 24 | + }); |
| 25 | + const body = await res.json(); |
| 26 | + |
| 27 | + expect(res.status).toEqual(200); |
| 28 | + expect(body.data.name).toBe("slack"); |
| 29 | + expect(body.data.isEnabled).toBe(true); |
| 30 | + expect(body.data.isConnected).toBe(true); |
| 31 | + expect(body.policies[0].abilities.read).toBe(true); |
| 32 | + expect(body.policies[0].abilities.update).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should require authorization", async () => { |
| 36 | + const team = await buildTeam(); |
| 37 | + const user = await buildUser(); |
| 38 | + const authenticationProviders = await team.getAuthenticationProviders(); |
| 39 | + |
| 40 | + const res = await server.post("/api/authenticationProviders.info", { |
| 41 | + body: { |
| 42 | + id: authenticationProviders[0].id, |
| 43 | + token: user.getJwtToken(), |
| 44 | + }, |
| 45 | + }); |
| 46 | + expect(res.status).toEqual(403); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should require authentication", async () => { |
| 50 | + const team = await buildTeam(); |
| 51 | + const authenticationProviders = await team.getAuthenticationProviders(); |
| 52 | + |
| 53 | + const res = await server.post("/api/authenticationProviders.info", { |
| 54 | + body: { |
| 55 | + id: authenticationProviders[0].id, |
| 56 | + }, |
| 57 | + }); |
| 58 | + expect(res.status).toEqual(401); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("#authenticationProviders.update", () => { |
| 63 | + it("should not allow admins to disable when last authentication provider", async () => { |
| 64 | + const team = await buildTeam(); |
| 65 | + const user = await buildAdmin({ teamId: team.id }); |
| 66 | + const authenticationProviders = await team.getAuthenticationProviders(); |
| 67 | + |
| 68 | + const res = await server.post("/api/authenticationProviders.update", { |
| 69 | + body: { |
| 70 | + id: authenticationProviders[0].id, |
| 71 | + isEnabled: false, |
| 72 | + token: user.getJwtToken(), |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + expect(res.status).toEqual(400); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should allow admins to disable", async () => { |
| 80 | + const team = await buildTeam(); |
| 81 | + const user = await buildAdmin({ teamId: team.id }); |
| 82 | + await team.createAuthenticationProvider({ |
| 83 | + name: "google", |
| 84 | + providerId: uuid.v4(), |
| 85 | + }); |
| 86 | + const authenticationProviders = await team.getAuthenticationProviders(); |
| 87 | + |
| 88 | + const res = await server.post("/api/authenticationProviders.update", { |
| 89 | + body: { |
| 90 | + id: authenticationProviders[0].id, |
| 91 | + isEnabled: false, |
| 92 | + token: user.getJwtToken(), |
| 93 | + }, |
| 94 | + }); |
| 95 | + const body = await res.json(); |
| 96 | + |
| 97 | + expect(res.status).toEqual(200); |
| 98 | + expect(body.data.name).toBe("slack"); |
| 99 | + expect(body.data.isEnabled).toBe(false); |
| 100 | + expect(body.data.isConnected).toBe(true); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should require authorization", async () => { |
| 104 | + const team = await buildTeam(); |
| 105 | + const user = await buildUser({ teamId: team.id }); |
| 106 | + const authenticationProviders = await team.getAuthenticationProviders(); |
| 107 | + |
| 108 | + const res = await server.post("/api/authenticationProviders.update", { |
| 109 | + body: { |
| 110 | + id: authenticationProviders[0].id, |
| 111 | + isEnabled: false, |
| 112 | + token: user.getJwtToken(), |
| 113 | + }, |
| 114 | + }); |
| 115 | + expect(res.status).toEqual(403); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should require authentication", async () => { |
| 119 | + const team = await buildTeam(); |
| 120 | + const authenticationProviders = await team.getAuthenticationProviders(); |
| 121 | + |
| 122 | + const res = await server.post("/api/authenticationProviders.update", { |
| 123 | + body: { |
| 124 | + id: authenticationProviders[0].id, |
| 125 | + isEnabled: false, |
| 126 | + }, |
| 127 | + }); |
| 128 | + expect(res.status).toEqual(401); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe("#authenticationProviders.list", () => { |
| 133 | + it("should return enabled and available auth providers", async () => { |
| 134 | + const team = await buildTeam(); |
| 135 | + const user = await buildUser({ teamId: team.id }); |
| 136 | + |
| 137 | + const res = await server.post("/api/authenticationProviders.list", { |
| 138 | + body: { token: user.getJwtToken() }, |
| 139 | + }); |
| 140 | + const body = await res.json(); |
| 141 | + |
| 142 | + expect(res.status).toEqual(200); |
| 143 | + expect(body.data.authenticationProviders.length).toBe(2); |
| 144 | + expect(body.data.authenticationProviders[0].name).toBe("slack"); |
| 145 | + expect(body.data.authenticationProviders[0].isEnabled).toBe(true); |
| 146 | + expect(body.data.authenticationProviders[0].isConnected).toBe(true); |
| 147 | + expect(body.data.authenticationProviders[1].name).toBe("google"); |
| 148 | + expect(body.data.authenticationProviders[1].isEnabled).toBe(false); |
| 149 | + expect(body.data.authenticationProviders[1].isConnected).toBe(false); |
| 150 | + }); |
| 151 | + |
| 152 | + it("should require authentication", async () => { |
| 153 | + const res = await server.post("/api/authenticationProviders.list"); |
| 154 | + expect(res.status).toEqual(401); |
| 155 | + }); |
| 156 | +}); |
0 commit comments