|
| 1 | +import { graphql } from 'testkit/gql'; |
| 2 | +import { ResourceAssignmentModeType } from 'testkit/gql/graphql'; |
| 3 | +import { execute } from 'testkit/graphql'; |
| 4 | +import { initSeed } from 'testkit/seed'; |
| 5 | + |
| 6 | +const AssignedResourcesSpec_CreateOIDCIntegrationMutation = graphql(` |
| 7 | + mutation AssignedResourcesSpec_CreateOIDCIntegrationMutation( |
| 8 | + $input: CreateOIDCIntegrationInput! |
| 9 | + ) { |
| 10 | + createOIDCIntegration(input: $input) { |
| 11 | + ok { |
| 12 | + createdOIDCIntegration { |
| 13 | + id |
| 14 | + defaultResourceAssignment { |
| 15 | + mode |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | +`); |
| 22 | + |
| 23 | +const AssignedResourcesSpec_ReadDefaultTest = graphql(` |
| 24 | + query AssignedResourcesSpec_ReadDefaultTest($organizationSlug: String!) { |
| 25 | + organization(reference: { bySelector: { organizationSlug: $organizationSlug } }) { |
| 26 | + id |
| 27 | + oidcIntegration { |
| 28 | + defaultResourceAssignment { |
| 29 | + mode |
| 30 | + projects { |
| 31 | + project { |
| 32 | + id |
| 33 | + slug |
| 34 | + } |
| 35 | + targets { |
| 36 | + mode |
| 37 | + targets { |
| 38 | + target { |
| 39 | + id |
| 40 | + slug |
| 41 | + } |
| 42 | + services { |
| 43 | + mode |
| 44 | + services |
| 45 | + } |
| 46 | + appDeployments { |
| 47 | + mode |
| 48 | + appDeployments |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +`); |
| 58 | + |
| 59 | +const AssignedResourcesSpec_UpdateDefaultMutation = graphql(` |
| 60 | + mutation AssignedResourcesSpec_UpdateDefaultMutation( |
| 61 | + $input: UpdateOIDCDefaultResourceAssignmentInput! |
| 62 | + ) { |
| 63 | + updateOIDCDefaultResourceAssignment(input: $input) { |
| 64 | + ok { |
| 65 | + updatedOIDCIntegration { |
| 66 | + id |
| 67 | + defaultResourceAssignment { |
| 68 | + mode |
| 69 | + projects { |
| 70 | + project { |
| 71 | + id |
| 72 | + slug |
| 73 | + } |
| 74 | + targets { |
| 75 | + mode |
| 76 | + targets { |
| 77 | + target { |
| 78 | + id |
| 79 | + slug |
| 80 | + } |
| 81 | + services { |
| 82 | + mode |
| 83 | + services |
| 84 | + } |
| 85 | + appDeployments { |
| 86 | + mode |
| 87 | + appDeployments |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + error { |
| 96 | + message |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +`); |
| 101 | + |
| 102 | +async function setup() { |
| 103 | + const { ownerToken, createOrg } = await initSeed().createOwner(); |
| 104 | + const { organization, createOrganizationAccessToken } = await createOrg(); |
| 105 | + |
| 106 | + const result = await execute({ |
| 107 | + document: AssignedResourcesSpec_CreateOIDCIntegrationMutation, |
| 108 | + variables: { |
| 109 | + input: { |
| 110 | + organizationId: organization.id, |
| 111 | + clientId: 'foo', |
| 112 | + clientSecret: 'foofoofoofoo', |
| 113 | + tokenEndpoint: 'http://localhost:8888/oauth/token', |
| 114 | + userinfoEndpoint: 'http://localhost:8888/oauth/userinfo', |
| 115 | + authorizationEndpoint: 'http://localhost:8888/oauth/authorize', |
| 116 | + }, |
| 117 | + }, |
| 118 | + authToken: ownerToken, |
| 119 | + }).then(r => r.expectNoGraphQLErrors()); |
| 120 | + |
| 121 | + // no default exists at creation |
| 122 | + expect(result.createOIDCIntegration.ok?.createdOIDCIntegration.defaultResourceAssignment).toBe( |
| 123 | + null, |
| 124 | + ); |
| 125 | + return { |
| 126 | + organization, |
| 127 | + ownerToken, |
| 128 | + oidcIntegrationId: result.createOIDCIntegration.ok?.createdOIDCIntegration.id!, |
| 129 | + createOrganizationAccessToken, |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +describe('read OIDC', () => { |
| 134 | + describe('permissions="organization:integrations"', () => { |
| 135 | + test.concurrent('success', async ({ expect }) => { |
| 136 | + const { organization, ownerToken, oidcIntegrationId } = await setup(); |
| 137 | + |
| 138 | + await execute({ |
| 139 | + document: AssignedResourcesSpec_UpdateDefaultMutation, |
| 140 | + variables: { |
| 141 | + input: { |
| 142 | + oidcIntegrationId, |
| 143 | + resources: { |
| 144 | + mode: ResourceAssignmentModeType.All, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + authToken: ownerToken, |
| 149 | + }).then(r => r.expectNoGraphQLErrors()); |
| 150 | + |
| 151 | + const read = await execute({ |
| 152 | + document: AssignedResourcesSpec_ReadDefaultTest, |
| 153 | + variables: { |
| 154 | + organizationSlug: organization.slug, |
| 155 | + }, |
| 156 | + authToken: ownerToken, |
| 157 | + }).then(r => r.expectNoGraphQLErrors()); |
| 158 | + |
| 159 | + expect(read).toEqual({ |
| 160 | + organization: { |
| 161 | + id: expect.stringMatching('.+'), |
| 162 | + oidcIntegration: { |
| 163 | + defaultResourceAssignment: { |
| 164 | + mode: 'ALL', |
| 165 | + projects: null, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + describe('permissions missing "organization:integrations"', () => { |
| 174 | + test.concurrent('fail', async ({ expect }) => { |
| 175 | + const { organization, ownerToken, oidcIntegrationId, createOrganizationAccessToken } = |
| 176 | + await setup(); |
| 177 | + const { privateAccessKey: readToken } = await createOrganizationAccessToken({ |
| 178 | + permissions: ['organization:read'], |
| 179 | + resources: { mode: ResourceAssignmentModeType.All }, |
| 180 | + }); |
| 181 | + |
| 182 | + await execute({ |
| 183 | + document: AssignedResourcesSpec_UpdateDefaultMutation, |
| 184 | + variables: { |
| 185 | + input: { |
| 186 | + oidcIntegrationId, |
| 187 | + resources: { |
| 188 | + mode: ResourceAssignmentModeType.All, |
| 189 | + }, |
| 190 | + }, |
| 191 | + }, |
| 192 | + authToken: ownerToken, |
| 193 | + }).then(r => r.expectNoGraphQLErrors()); |
| 194 | + |
| 195 | + const errors = await execute({ |
| 196 | + document: AssignedResourcesSpec_ReadDefaultTest, |
| 197 | + variables: { |
| 198 | + organizationSlug: organization.slug, |
| 199 | + }, |
| 200 | + authToken: readToken, |
| 201 | + }).then(r => r.expectGraphQLErrors()); |
| 202 | + expect(errors).toHaveLength(1); |
| 203 | + expect(errors).toMatchObject([ |
| 204 | + { |
| 205 | + extensions: { |
| 206 | + code: 'UNAUTHORISED', |
| 207 | + }, |
| 208 | + message: `No access (reason: "Missing permission for performing 'organization:describe' on resource")`, |
| 209 | + path: ['organization'], |
| 210 | + }, |
| 211 | + ]); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
| 215 | + |
| 216 | +describe('update OIDC default assigned resources', () => { |
| 217 | + describe('permissions="oidc:modify"', () => { |
| 218 | + test.concurrent('success', async ({ expect }) => { |
| 219 | + const { ownerToken, oidcIntegrationId } = await setup(); |
| 220 | + |
| 221 | + const update = await execute({ |
| 222 | + document: AssignedResourcesSpec_UpdateDefaultMutation, |
| 223 | + variables: { |
| 224 | + input: { |
| 225 | + oidcIntegrationId, |
| 226 | + resources: { |
| 227 | + mode: ResourceAssignmentModeType.All, |
| 228 | + }, |
| 229 | + }, |
| 230 | + }, |
| 231 | + authToken: ownerToken, |
| 232 | + }).then(r => r.expectNoGraphQLErrors()); |
| 233 | + |
| 234 | + expect(update).toEqual({ |
| 235 | + updateOIDCDefaultResourceAssignment: { |
| 236 | + error: null, |
| 237 | + ok: { |
| 238 | + updatedOIDCIntegration: { |
| 239 | + defaultResourceAssignment: { |
| 240 | + mode: 'ALL', |
| 241 | + projects: null, |
| 242 | + }, |
| 243 | + id: expect.stringMatching('.+'), |
| 244 | + }, |
| 245 | + }, |
| 246 | + }, |
| 247 | + }); |
| 248 | + }); |
| 249 | + }); |
| 250 | + |
| 251 | + describe('permissions missing "oidc:modify"', () => { |
| 252 | + test.concurrent('fails', async ({ expect }) => { |
| 253 | + const { createOrganizationAccessToken, ownerToken, oidcIntegrationId } = await setup(); |
| 254 | + |
| 255 | + const { privateAccessKey: accessToken } = await createOrganizationAccessToken({ |
| 256 | + permissions: ['organization:read'], |
| 257 | + resources: { |
| 258 | + mode: ResourceAssignmentModeType.All, |
| 259 | + }, |
| 260 | + }); |
| 261 | + |
| 262 | + const errors = await execute({ |
| 263 | + document: AssignedResourcesSpec_UpdateDefaultMutation, |
| 264 | + variables: { |
| 265 | + input: { |
| 266 | + oidcIntegrationId, |
| 267 | + resources: { |
| 268 | + mode: ResourceAssignmentModeType.All, |
| 269 | + }, |
| 270 | + }, |
| 271 | + }, |
| 272 | + authToken: accessToken, |
| 273 | + }).then(r => r.expectGraphQLErrors()); |
| 274 | + expect(errors).toHaveLength(1); |
| 275 | + expect(errors).toMatchObject([ |
| 276 | + { |
| 277 | + extensions: { |
| 278 | + code: 'UNAUTHORISED', |
| 279 | + }, |
| 280 | + message: `No access (reason: "Missing permission for performing 'oidc:modify' on resource")`, |
| 281 | + path: ['updateOIDCDefaultResourceAssignment'], |
| 282 | + }, |
| 283 | + ]); |
| 284 | + }); |
| 285 | + }); |
| 286 | +}); |
0 commit comments