|
| 1 | +import { click, find, waitFor } from '@ember/test-helpers'; |
| 2 | +import { module, test } from 'qunit'; |
| 3 | + |
| 4 | +import format from 'date-fns/format'; |
| 5 | + |
| 6 | +import { setupApplicationTest } from 'crates-io/tests/helpers'; |
| 7 | + |
| 8 | +import { visit } from '../helpers/visit-ignoring-abort'; |
| 9 | + |
| 10 | +module('Acceptance | sudo', function (hooks) { |
| 11 | + setupApplicationTest(hooks); |
| 12 | + |
| 13 | + const prepare = context => { |
| 14 | + const user = context.server.create('user', { |
| 15 | + login: 'johnnydee', |
| 16 | + name: 'John Doe', |
| 17 | + |
| 18 | + avatar: 'https://avatars2.githubusercontent.com/u/1234567?v=4', |
| 19 | + isAdmin: true, |
| 20 | + }); |
| 21 | + |
| 22 | + const crate = context.server.create('crate', { |
| 23 | + name: 'foo', |
| 24 | + newest_version: '0.1.0', |
| 25 | + }); |
| 26 | + |
| 27 | + const version = context.server.create('version', { |
| 28 | + crate, |
| 29 | + num: '0.1.0', |
| 30 | + }); |
| 31 | + |
| 32 | + context.authenticateAs(user); |
| 33 | + return { user, crate, version }; |
| 34 | + }; |
| 35 | + |
| 36 | + test('admin user is not initially in sudo mode', async function (assert) { |
| 37 | + prepare(this); |
| 38 | + |
| 39 | + await visit('/crates/foo/versions'); |
| 40 | + |
| 41 | + // Test the various header elements. |
| 42 | + assert.dom('[data-test-wizard-hat]').doesNotExist(); |
| 43 | + assert.dom('[data-test-disable-admin-actions]').doesNotExist(); |
| 44 | + assert.dom('[data-test-enable-admin-actions]').exists(); |
| 45 | + |
| 46 | + // Test that the fieldset is present and disabled. |
| 47 | + assert.dom('[data-test-placeholder-fieldset]').exists().isDisabled(); |
| 48 | + |
| 49 | + // From the perspective of the actual button, it isn't disabled, even though |
| 50 | + // the fieldset effectively makes it unclickable. |
| 51 | + assert.dom('[data-test-version-yank-button="0.1.0"]').exists(); |
| 52 | + }); |
| 53 | + |
| 54 | + test('admin user can enter sudo mode', async function (assert) { |
| 55 | + prepare(this); |
| 56 | + |
| 57 | + await visit('/crates/foo/versions'); |
| 58 | + |
| 59 | + const untilAbout = Date.now() + 6 * 60 * 60 * 1000; |
| 60 | + await click('[data-test-enable-admin-actions]'); |
| 61 | + |
| 62 | + // Test the various header elements. |
| 63 | + assert.dom('[data-test-wizard-hat]').exists(); |
| 64 | + assert.dom('[data-test-disable-admin-actions]').exists(); |
| 65 | + assert.dom('[data-test-enable-admin-actions]').doesNotExist(); |
| 66 | + |
| 67 | + // Test that the expiry time is sensible. We'll allow a minute either way in |
| 68 | + // case of slow tests or slightly wonky clocks. |
| 69 | + const disable = find('[data-test-disable-admin-actions] > div'); |
| 70 | + let seen = 0; |
| 71 | + for (const ts of [untilAbout - 60 * 1000, untilAbout, untilAbout + 60 * 1000]) { |
| 72 | + const time = format(new Date(ts), 'HH:mm'); |
| 73 | + if (disable.textContent.includes(time)) { |
| 74 | + seen += 1; |
| 75 | + } |
| 76 | + } |
| 77 | + assert.strictEqual(seen, 1); |
| 78 | + |
| 79 | + // Test that the fieldset is not present. |
| 80 | + assert.dom('[data-test-placeholder-fieldset]').doesNotExist(); |
| 81 | + assert.dom('[data-test-version-yank-button="0.1.0"]').exists(); |
| 82 | + }); |
| 83 | + |
| 84 | + test('admin can yank a crate in sudo mode', async function (assert) { |
| 85 | + this.server.loadFixtures(); |
| 86 | + prepare(this); |
| 87 | + |
| 88 | + await visit('/crates/foo/versions'); |
| 89 | + await click('[data-test-enable-admin-actions]'); |
| 90 | + |
| 91 | + await click('[data-test-version-yank-button="0.1.0"]'); |
| 92 | + |
| 93 | + await waitFor('[data-test-version-unyank-button="0.1.0"]'); |
| 94 | + assert.dom('[data-test-version-unyank-button="0.1.0"]').exists(); |
| 95 | + await click('[data-test-version-unyank-button="0.1.0"]'); |
| 96 | + |
| 97 | + await waitFor('[data-test-version-yank-button="0.1.0"]'); |
| 98 | + assert.dom('[data-test-version-yank-button="0.1.0"]').exists(); |
| 99 | + }); |
| 100 | +}); |
0 commit comments