|
| 1 | +import { module, test } from 'qunit'; |
| 2 | + |
| 3 | +import fetch from 'fetch'; |
| 4 | + |
| 5 | +import { setupTest } from '../../helpers'; |
| 6 | +import setupMirage from '../../helpers/setup-mirage'; |
| 7 | + |
| 8 | +const ADD_USER_BODY = JSON.stringify({ owners: ['john-doe'] }); |
| 9 | + |
| 10 | +module('Mirage | PUT /api/v1/crates/:name/owners', function (hooks) { |
| 11 | + setupTest(hooks); |
| 12 | + setupMirage(hooks); |
| 13 | + |
| 14 | + test('returns 403 if unauthenticated', async function (assert) { |
| 15 | + let response = await fetch('/api/v1/crates/foo/owners', { method: 'PUT', body: ADD_USER_BODY }); |
| 16 | + assert.strictEqual(response.status, 403); |
| 17 | + assert.deepEqual(await response.json(), { |
| 18 | + errors: [{ detail: 'must be logged in to perform that action' }], |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + test('returns 404 for unknown crates', async function (assert) { |
| 23 | + let user = this.server.create('user'); |
| 24 | + this.authenticateAs(user); |
| 25 | + |
| 26 | + let response = await fetch('/api/v1/crates/foo/owners', { method: 'PUT', body: ADD_USER_BODY }); |
| 27 | + assert.strictEqual(response.status, 404); |
| 28 | + assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] }); |
| 29 | + }); |
| 30 | + |
| 31 | + test('can add new owner', async function (assert) { |
| 32 | + let user = this.server.create('user'); |
| 33 | + this.authenticateAs(user); |
| 34 | + |
| 35 | + let crate = this.server.create('crate', { name: 'foo' }); |
| 36 | + this.server.create('crate-ownership', { crate, user }); |
| 37 | + |
| 38 | + let user2 = this.server.create('user'); |
| 39 | + |
| 40 | + let body = JSON.stringify({ owners: [user2.login] }); |
| 41 | + let response = await fetch('/api/v1/crates/foo/owners', { method: 'PUT', body }); |
| 42 | + assert.strictEqual(response.status, 200); |
| 43 | + assert.deepEqual(await response.json(), { |
| 44 | + ok: true, |
| 45 | + msg: 'user user-2 has been invited to be an owner of crate foo', |
| 46 | + }); |
| 47 | + |
| 48 | + let owners = this.server.schema.crateOwnerships.where({ crateId: crate.id }); |
| 49 | + assert.strictEqual(owners.length, 1); |
| 50 | + assert.strictEqual(owners.models[0].userId, user.id); |
| 51 | + |
| 52 | + let invites = this.server.schema.crateOwnerInvitations.where({ crateId: crate.id }); |
| 53 | + assert.strictEqual(invites.length, 1); |
| 54 | + assert.strictEqual(invites.models[0].inviterId, user.id); |
| 55 | + assert.strictEqual(invites.models[0].inviteeId, user2.id); |
| 56 | + }); |
| 57 | + |
| 58 | + test('can add team owner', async function (assert) { |
| 59 | + let user = this.server.create('user'); |
| 60 | + this.authenticateAs(user); |
| 61 | + |
| 62 | + let crate = this.server.create('crate', { name: 'foo' }); |
| 63 | + this.server.create('crate-ownership', { crate, user }); |
| 64 | + |
| 65 | + let team = this.server.create('team'); |
| 66 | + |
| 67 | + let body = JSON.stringify({ owners: [team.login] }); |
| 68 | + let response = await fetch('/api/v1/crates/foo/owners', { method: 'PUT', body }); |
| 69 | + assert.strictEqual(response.status, 200); |
| 70 | + assert.deepEqual(await response.json(), { |
| 71 | + ok: true, |
| 72 | + msg: 'team github:rust-lang:team-1 has been added as an owner of crate foo', |
| 73 | + }); |
| 74 | + |
| 75 | + let owners = this.server.schema.crateOwnerships.where({ crateId: crate.id }); |
| 76 | + assert.strictEqual(owners.length, 2); |
| 77 | + assert.strictEqual(owners.models[0].userId, user.id); |
| 78 | + assert.strictEqual(owners.models[0].teamId, null); |
| 79 | + assert.strictEqual(owners.models[1].userId, null); |
| 80 | + assert.strictEqual(owners.models[1].teamId, user.id); |
| 81 | + |
| 82 | + let invites = this.server.schema.crateOwnerInvitations.where({ crateId: crate.id }); |
| 83 | + assert.strictEqual(invites.length, 0); |
| 84 | + }); |
| 85 | + |
| 86 | + test('can add multiple owners', async function (assert) { |
| 87 | + let user = this.server.create('user'); |
| 88 | + this.authenticateAs(user); |
| 89 | + |
| 90 | + let crate = this.server.create('crate', { name: 'foo' }); |
| 91 | + this.server.create('crate-ownership', { crate, user }); |
| 92 | + |
| 93 | + let team = this.server.create('team'); |
| 94 | + let user2 = this.server.create('user'); |
| 95 | + let user3 = this.server.create('user'); |
| 96 | + |
| 97 | + let body = JSON.stringify({ owners: [user2.login, team.login, user3.login] }); |
| 98 | + let response = await fetch('/api/v1/crates/foo/owners', { method: 'PUT', body }); |
| 99 | + assert.strictEqual(response.status, 200); |
| 100 | + assert.deepEqual(await response.json(), { |
| 101 | + ok: true, |
| 102 | + msg: 'user user-2 has been invited to be an owner of crate foo,team github:rust-lang:team-1 has been added as an owner of crate foo,user user-3 has been invited to be an owner of crate foo', |
| 103 | + }); |
| 104 | + |
| 105 | + let owners = this.server.schema.crateOwnerships.where({ crateId: crate.id }); |
| 106 | + assert.strictEqual(owners.length, 2); |
| 107 | + assert.strictEqual(owners.models[0].userId, user.id); |
| 108 | + assert.strictEqual(owners.models[0].teamId, null); |
| 109 | + assert.strictEqual(owners.models[1].userId, null); |
| 110 | + assert.strictEqual(owners.models[1].teamId, user.id); |
| 111 | + |
| 112 | + let invites = this.server.schema.crateOwnerInvitations.where({ crateId: crate.id }); |
| 113 | + assert.strictEqual(invites.length, 2); |
| 114 | + assert.strictEqual(invites.models[0].inviterId, user.id); |
| 115 | + assert.strictEqual(invites.models[0].inviteeId, user2.id); |
| 116 | + assert.strictEqual(invites.models[1].inviterId, user.id); |
| 117 | + assert.strictEqual(invites.models[1].inviteeId, user3.id); |
| 118 | + }); |
| 119 | +}); |
0 commit comments