|
| 1 | +import { module, test } from 'qunit'; |
| 2 | +import { setupApplicationTest } from 'ember-qunit'; |
| 3 | +import { currentURL, findAll, click, fillIn } from '@ember/test-helpers'; |
| 4 | +import window, { setupWindowMock } from 'ember-window-mock'; |
| 5 | +import { Response } from 'ember-cli-mirage'; |
| 6 | + |
| 7 | +import setupMirage from '../helpers/setup-mirage'; |
| 8 | +import { visit } from '../helpers/visit-ignoring-abort'; |
| 9 | + |
| 10 | +module('Acceptance | api-tokens', function(hooks) { |
| 11 | + setupApplicationTest(hooks); |
| 12 | + setupWindowMock(hooks); |
| 13 | + setupMirage(hooks); |
| 14 | + |
| 15 | + function prepare(context) { |
| 16 | + window.localStorage.setItem('isLoggedIn', '1'); |
| 17 | + |
| 18 | + context.server.get('/api/v1/me', { |
| 19 | + user: { |
| 20 | + id: 42, |
| 21 | + login: 'johnnydee', |
| 22 | + email_verified: true, |
| 23 | + email_verification_sent: true, |
| 24 | + name: 'John Doe', |
| 25 | + |
| 26 | + avatar: 'https://avatars2.githubusercontent.com/u/1234567?v=4', |
| 27 | + url: 'https://github.com/johnnydee', |
| 28 | + }, |
| 29 | + owned_crates: [], |
| 30 | + }); |
| 31 | + |
| 32 | + context.server.get('/api/v1/me/tokens', { |
| 33 | + api_tokens: [ |
| 34 | + { id: 2, name: 'BAR', created_at: new Date('2017-11-19T17:59:22').toISOString(), last_used_at: null }, |
| 35 | + { |
| 36 | + id: 1, |
| 37 | + name: 'foo', |
| 38 | + created_at: new Date('2017-08-01T12:34:56').toISOString(), |
| 39 | + last_used_at: new Date('2017-11-02T01:45:14').toISOString(), |
| 40 | + }, |
| 41 | + ], |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + test('/me is showing the list of active API tokens', async function(assert) { |
| 46 | + prepare(this); |
| 47 | + |
| 48 | + await visit('/me'); |
| 49 | + assert.equal(currentURL(), '/me'); |
| 50 | + assert.dom('[data-test-api-token]').exists({ count: 2 }); |
| 51 | + |
| 52 | + let [row1, row2] = findAll('[data-test-api-token]'); |
| 53 | + assert.dom('[data-test-name]', row1).hasText('BAR'); |
| 54 | + assert.dom('[data-test-created-at]', row1).hasText('Created 18 hours ago'); |
| 55 | + assert.dom('[data-test-last-used-at]', row1).hasText('Never used'); |
| 56 | + assert.dom('[data-test-save-token-button]', row1).doesNotExist(); |
| 57 | + assert.dom('[data-test-revoke-token-button]', row1).exists(); |
| 58 | + assert.dom('[data-test-saving-spinner]', row1).doesNotExist(); |
| 59 | + assert.dom('[data-test-error]', row1).doesNotExist(); |
| 60 | + assert.dom('[data-test-token]', row1).doesNotExist(); |
| 61 | + |
| 62 | + assert.dom('[data-test-name]', row2).hasText('foo'); |
| 63 | + assert.dom('[data-test-created-at]', row2).hasText('Created 4 months ago'); |
| 64 | + assert.dom('[data-test-last-used-at]', row2).hasText('Last used 18 days ago'); |
| 65 | + assert.dom('[data-test-save-token-button]', row2).doesNotExist(); |
| 66 | + assert.dom('[data-test-revoke-token-button]', row2).exists(); |
| 67 | + assert.dom('[data-test-saving-spinner]', row2).doesNotExist(); |
| 68 | + assert.dom('[data-test-error]', row2).doesNotExist(); |
| 69 | + assert.dom('[data-test-token]', row2).doesNotExist(); |
| 70 | + }); |
| 71 | + |
| 72 | + test('API tokens can be revoked', async function(assert) { |
| 73 | + prepare(this); |
| 74 | + |
| 75 | + this.server.delete('/api/v1/me/tokens/:id', function(schema, request) { |
| 76 | + assert.step(`delete id:${request.params.id}`); |
| 77 | + return {}; |
| 78 | + }); |
| 79 | + |
| 80 | + await visit('/me'); |
| 81 | + assert.equal(currentURL(), '/me'); |
| 82 | + assert.dom('[data-test-api-token]').exists({ count: 2 }); |
| 83 | + |
| 84 | + await click('[data-test-api-token="1"] [data-test-revoke-token-button]'); |
| 85 | + assert.verifySteps(['delete id:1']); |
| 86 | + |
| 87 | + assert.dom('[data-test-api-token]').exists({ count: 1 }); |
| 88 | + assert.dom('[data-test-api-token="2"]').exists(); |
| 89 | + assert.dom('[data-test-error]').doesNotExist(); |
| 90 | + }); |
| 91 | + |
| 92 | + test('failed API tokens revocation shows an error', async function(assert) { |
| 93 | + prepare(this); |
| 94 | + |
| 95 | + this.server.delete('/api/v1/me/tokens/:id', function() { |
| 96 | + return new Response(500, {}, {}); |
| 97 | + }); |
| 98 | + |
| 99 | + await visit('/me'); |
| 100 | + assert.equal(currentURL(), '/me'); |
| 101 | + assert.dom('[data-test-api-token]').exists({ count: 2 }); |
| 102 | + |
| 103 | + await click('[data-test-api-token="1"] [data-test-revoke-token-button]'); |
| 104 | + assert.dom('[data-test-api-token]').exists({ count: 2 }); |
| 105 | + assert.dom('[data-test-api-token="2"]').exists(); |
| 106 | + assert.dom('[data-test-api-token="1"]').exists(); |
| 107 | + assert.dom('[data-test-error]').includesText('An error occurred while revoking this token'); |
| 108 | + }); |
| 109 | + |
| 110 | + test('new API tokens can be created', async function(assert) { |
| 111 | + prepare(this); |
| 112 | + |
| 113 | + this.server.put('/api/v1/me/tokens', function(schema, request) { |
| 114 | + assert.step('put'); |
| 115 | + |
| 116 | + let { api_token } = JSON.parse(request.requestBody); |
| 117 | + |
| 118 | + return { |
| 119 | + api_token: { |
| 120 | + id: 5, |
| 121 | + name: api_token.name, |
| 122 | + token: 'zuz6nYcXJOzPDvnA9vucNwccG0lFSGbh', |
| 123 | + revoked: false, |
| 124 | + created_at: api_token.created_at, |
| 125 | + last_used_at: api_token.last_used_at, |
| 126 | + }, |
| 127 | + }; |
| 128 | + }); |
| 129 | + |
| 130 | + await visit('/me'); |
| 131 | + assert.equal(currentURL(), '/me'); |
| 132 | + assert.dom('[data-test-api-token]').exists({ count: 2 }); |
| 133 | + assert.dom('[data-test-focused-input]').doesNotExist(); |
| 134 | + assert.dom('[data-test-save-token-button]').doesNotExist(); |
| 135 | + |
| 136 | + await click('[data-test-new-token-button]'); |
| 137 | + assert.dom('[data-test-new-token-button]').isDisabled(); |
| 138 | + assert.dom('[data-test-focused-input]').exists(); |
| 139 | + assert.dom('[data-test-save-token-button]').exists(); |
| 140 | + |
| 141 | + await fillIn('[data-test-focused-input]', 'the new token'); |
| 142 | + await click('[data-test-save-token-button]'); |
| 143 | + assert.verifySteps(['put']); |
| 144 | + assert.dom('[data-test-focused-input]').doesNotExist(); |
| 145 | + assert.dom('[data-test-save-token-button]').doesNotExist(); |
| 146 | + |
| 147 | + assert.dom('[data-test-api-token="5"] [data-test-name]').hasText('the new token'); |
| 148 | + assert.dom('[data-test-api-token="5"] [data-test-save-token-button]').doesNotExist(); |
| 149 | + assert.dom('[data-test-api-token="5"] [data-test-revoke-token-button]').exists(); |
| 150 | + assert.dom('[data-test-api-token="5"] [data-test-saving-spinner]').doesNotExist(); |
| 151 | + assert.dom('[data-test-api-token="5"] [data-test-error]').doesNotExist(); |
| 152 | + assert.dom('[data-test-token]').includesText('cargo login zuz6nYcXJOzPDvnA9vucNwccG0lFSGbh'); |
| 153 | + }); |
| 154 | +}); |
0 commit comments