|
| 1 | +import { bootstrap } from "@/bootstrap"; |
| 2 | +import { AppModule } from "@/app.module"; |
| 3 | +import { PrismaModule } from "@/modules/prisma/prisma.module"; |
| 4 | +import { TokensModule } from "@/modules/tokens/tokens.module"; |
| 5 | +import { UsersModule } from "@/modules/users/users.module"; |
| 6 | +import { INestApplication } from "@nestjs/common"; |
| 7 | +import { NestExpressApplication } from "@nestjs/platform-express"; |
| 8 | +import { Test } from "@nestjs/testing"; |
| 9 | +import request from "supertest"; |
| 10 | +import { MembershipRepositoryFixture } from "test/fixtures/repository/membership.repository.fixture"; |
| 11 | +import { TeamRepositoryFixture } from "test/fixtures/repository/team.repository.fixture"; |
| 12 | +import { UserRepositoryFixture } from "test/fixtures/repository/users.repository.fixture"; |
| 13 | +import { randomString } from "test/utils/randomString"; |
| 14 | +import { withApiAuth } from "test/utils/withApiAuth"; |
| 15 | + |
| 16 | +import { SUCCESS_STATUS } from "@calcom/platform-constants"; |
| 17 | +import type { Team, User } from "@calcom/prisma/client"; |
| 18 | + |
| 19 | +describe("Teams Invite Endpoints", () => { |
| 20 | + describe("User Authentication - User is Team Admin", () => { |
| 21 | + let app: INestApplication; |
| 22 | + |
| 23 | + let userRepositoryFixture: UserRepositoryFixture; |
| 24 | + let teamsRepositoryFixture: TeamRepositoryFixture; |
| 25 | + let membershipsRepositoryFixture: MembershipRepositoryFixture; |
| 26 | + |
| 27 | + let team: Team; |
| 28 | + |
| 29 | + const userEmail = `teams-invite-admin-${randomString()}@api.com`; |
| 30 | + |
| 31 | + let user: User; |
| 32 | + |
| 33 | + beforeAll(async () => { |
| 34 | + const moduleRef = await withApiAuth( |
| 35 | + userEmail, |
| 36 | + Test.createTestingModule({ |
| 37 | + imports: [AppModule, PrismaModule, UsersModule, TokensModule], |
| 38 | + }) |
| 39 | + ).compile(); |
| 40 | + |
| 41 | + userRepositoryFixture = new UserRepositoryFixture(moduleRef); |
| 42 | + teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef); |
| 43 | + membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef); |
| 44 | + |
| 45 | + user = await userRepositoryFixture.create({ |
| 46 | + email: userEmail, |
| 47 | + username: userEmail, |
| 48 | + }); |
| 49 | + |
| 50 | + team = await teamsRepositoryFixture.create({ |
| 51 | + name: `teams-invite-team-${randomString()}`, |
| 52 | + isOrganization: false, |
| 53 | + }); |
| 54 | + |
| 55 | + // Admin of the team |
| 56 | + await membershipsRepositoryFixture.create({ |
| 57 | + role: "ADMIN", |
| 58 | + user: { connect: { id: user.id } }, |
| 59 | + team: { connect: { id: team.id } }, |
| 60 | + }); |
| 61 | + |
| 62 | + app = moduleRef.createNestApplication(); |
| 63 | + bootstrap(app as NestExpressApplication); |
| 64 | + await app.init(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should create a team invite", async () => { |
| 68 | + return request(app.getHttpServer()) |
| 69 | + .post(`/v2/teams/${team.id}/invite`) |
| 70 | + .expect(200) |
| 71 | + .then((response) => { |
| 72 | + expect(response.body.status).toEqual(SUCCESS_STATUS); |
| 73 | + expect(response.body.data.token.length).toBeGreaterThan(0); |
| 74 | + expect(response.body.data.inviteLink).toEqual(expect.any(String)); |
| 75 | + expect(response.body.data.inviteLink).toContain(response.body.data.token); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should create a new invite on each request", async () => { |
| 80 | + const first = await request(app.getHttpServer()).post(`/v2/teams/${team.id}/invite`).expect(200); |
| 81 | + const firstToken = first.body.data.token as string; |
| 82 | + |
| 83 | + return request(app.getHttpServer()) |
| 84 | + .post(`/v2/teams/${team.id}/invite`) |
| 85 | + .expect(200) |
| 86 | + .then((response) => { |
| 87 | + expect(response.body.status).toEqual(SUCCESS_STATUS); |
| 88 | + expect(response.body.data.token).not.toEqual(firstToken); |
| 89 | + expect(response.body.data.inviteLink).toEqual(expect.any(String)); |
| 90 | + expect(response.body.data.inviteLink).toContain(response.body.data.token); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + afterAll(async () => { |
| 95 | + await userRepositoryFixture.deleteByEmail(user.email); |
| 96 | + await teamsRepositoryFixture.delete(team.id); |
| 97 | + await app.close(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe("User Authentication - User is Team Member (not Admin)", () => { |
| 102 | + let app: INestApplication; |
| 103 | + |
| 104 | + let userRepositoryFixture: UserRepositoryFixture; |
| 105 | + let teamsRepositoryFixture: TeamRepositoryFixture; |
| 106 | + let membershipsRepositoryFixture: MembershipRepositoryFixture; |
| 107 | + |
| 108 | + let team: Team; |
| 109 | + |
| 110 | + const userEmail = `teams-invite-member-${randomString()}@api.com`; |
| 111 | + |
| 112 | + let user: User; |
| 113 | + |
| 114 | + beforeAll(async () => { |
| 115 | + const moduleRef = await withApiAuth( |
| 116 | + userEmail, |
| 117 | + Test.createTestingModule({ |
| 118 | + imports: [AppModule, PrismaModule, UsersModule, TokensModule], |
| 119 | + }) |
| 120 | + ).compile(); |
| 121 | + |
| 122 | + userRepositoryFixture = new UserRepositoryFixture(moduleRef); |
| 123 | + teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef); |
| 124 | + membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef); |
| 125 | + |
| 126 | + user = await userRepositoryFixture.create({ |
| 127 | + email: userEmail, |
| 128 | + username: userEmail, |
| 129 | + }); |
| 130 | + |
| 131 | + team = await teamsRepositoryFixture.create({ |
| 132 | + name: `teams-invite-member-team-${randomString()}`, |
| 133 | + isOrganization: false, |
| 134 | + }); |
| 135 | + |
| 136 | + // Regular member of the team (not admin) |
| 137 | + await membershipsRepositoryFixture.create({ |
| 138 | + role: "MEMBER", |
| 139 | + user: { connect: { id: user.id } }, |
| 140 | + team: { connect: { id: team.id } }, |
| 141 | + }); |
| 142 | + |
| 143 | + app = moduleRef.createNestApplication(); |
| 144 | + bootstrap(app as NestExpressApplication); |
| 145 | + await app.init(); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should fail to create invite as non-admin member", async () => { |
| 149 | + return request(app.getHttpServer()).post(`/v2/teams/${team.id}/invite`).expect(403); |
| 150 | + }); |
| 151 | + |
| 152 | + afterAll(async () => { |
| 153 | + await userRepositoryFixture.deleteByEmail(user.email); |
| 154 | + await teamsRepositoryFixture.delete(team.id); |
| 155 | + await app.close(); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + describe("User Authentication - User is not a Team Member", () => { |
| 160 | + let app: INestApplication; |
| 161 | + |
| 162 | + let userRepositoryFixture: UserRepositoryFixture; |
| 163 | + let teamsRepositoryFixture: TeamRepositoryFixture; |
| 164 | + |
| 165 | + let team: Team; |
| 166 | + |
| 167 | + const userEmail = `teams-invite-non-member-${randomString()}@api.com`; |
| 168 | + |
| 169 | + let user: User; |
| 170 | + |
| 171 | + beforeAll(async () => { |
| 172 | + const moduleRef = await withApiAuth( |
| 173 | + userEmail, |
| 174 | + Test.createTestingModule({ |
| 175 | + imports: [AppModule, PrismaModule, UsersModule, TokensModule], |
| 176 | + }) |
| 177 | + ).compile(); |
| 178 | + |
| 179 | + userRepositoryFixture = new UserRepositoryFixture(moduleRef); |
| 180 | + teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef); |
| 181 | + |
| 182 | + user = await userRepositoryFixture.create({ |
| 183 | + email: userEmail, |
| 184 | + username: userEmail, |
| 185 | + }); |
| 186 | + |
| 187 | + team = await teamsRepositoryFixture.create({ |
| 188 | + name: `teams-invite-non-member-team-${randomString()}`, |
| 189 | + isOrganization: false, |
| 190 | + }); |
| 191 | + |
| 192 | + // User is NOT a member of this team |
| 193 | + |
| 194 | + app = moduleRef.createNestApplication(); |
| 195 | + bootstrap(app as NestExpressApplication); |
| 196 | + await app.init(); |
| 197 | + }); |
| 198 | + |
| 199 | + it("should fail to create invite as non-member", async () => { |
| 200 | + return request(app.getHttpServer()).post(`/v2/teams/${team.id}/invite`).expect(403); |
| 201 | + }); |
| 202 | + |
| 203 | + afterAll(async () => { |
| 204 | + await userRepositoryFixture.deleteByEmail(user.email); |
| 205 | + await teamsRepositoryFixture.delete(team.id); |
| 206 | + await app.close(); |
| 207 | + }); |
| 208 | + }); |
| 209 | +}); |
0 commit comments