Skip to content

Commit 213b184

Browse files
authored
Add sudo mode acceptance tests, per #8210 feedback (#8237)
1 parent 4a29008 commit 213b184

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

app/components/header.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<Dropdown data-test-user-menu as |dd|>
2323
<dd.Trigger local-class="dropdown-button" data-test-toggle>
2424
{{#if this.session.isSudoEnabled}}
25-
<div local-class="wizard-hat">🧙</div>
25+
<div data-test-wizard-hat local-class="wizard-hat">🧙</div>
2626
{{/if}}
2727
<UserAvatar @user={{this.session.currentUser}} @size="small" local-class="avatar" data-test-avatar />
2828
{{ this.session.currentUser.name }}
@@ -35,12 +35,12 @@
3535
{{#if this.session.isAdmin}}
3636
<menu.Item local-class='sudo'>
3737
{{#if this.session.isSudoEnabled}}
38-
<button local-class='sudo-menu-item' type='button' {{on 'click' this.disableSudo}}>
38+
<button data-test-disable-admin-actions local-class='sudo-menu-item' type='button' {{on 'click' this.disableSudo}}>
3939
Disable admin actions
4040
<div local-class='expires-in'>expires at {{date-format this.session.sudoEnabledUntil 'HH:mm'}}</div>
4141
</button>
4242
{{else}}
43-
<button local-class='sudo-menu-item' type='button' {{on 'click' this.enableSudo}}>
43+
<button data-test-enable-admin-actions local-class='sudo-menu-item' type='button' {{on 'click' this.enableSudo}}>
4444
Enable admin actions
4545
</button>
4646
{{/if}}

app/components/privileged-action.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</div>
1010
{{else}}
1111
<div local-class='placeholder'>
12-
<fieldset disabled="disabled">
12+
<fieldset data-test-placeholder-fieldset disabled="disabled">
1313
{{yield}}
1414
</fieldset>
1515
<EmberTooltip>

tests/acceptance/sudo-test.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
function prepare(context, isAdmin) {
14+
const user = context.server.create('user', {
15+
login: 'johnnydee',
16+
name: 'John Doe',
17+
email: 'john@doe.com',
18+
avatar: 'https://avatars2.githubusercontent.com/u/1234567?v=4',
19+
isAdmin,
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('non-admin users do not see any controls', async function (assert) {
37+
prepare(this, false);
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]').doesNotExist();
45+
46+
// Assert that there's no yank button, disabled, enabled, or in any state.
47+
assert.dom('[data-test-version-yank-button="0.1.0"]').doesNotExist();
48+
});
49+
50+
test('admin user is not initially in sudo mode', async function (assert) {
51+
prepare(this, true);
52+
53+
await visit('/crates/foo/versions');
54+
55+
// Test the various header elements.
56+
assert.dom('[data-test-wizard-hat]').doesNotExist();
57+
assert.dom('[data-test-disable-admin-actions]').doesNotExist();
58+
assert.dom('[data-test-enable-admin-actions]').exists();
59+
60+
// Test that the fieldset is present and disabled.
61+
assert.dom('[data-test-placeholder-fieldset]').exists().isDisabled();
62+
63+
// From the perspective of the actual button, it isn't disabled, even though
64+
// the fieldset effectively makes it unclickable.
65+
assert.dom('[data-test-version-yank-button="0.1.0"]').exists();
66+
});
67+
68+
test('admin user can enter sudo mode', async function (assert) {
69+
prepare(this, true);
70+
71+
await visit('/crates/foo/versions');
72+
73+
const untilAbout = Date.now() + 6 * 60 * 60 * 1000;
74+
await click('[data-test-enable-admin-actions]');
75+
76+
// Test the various header elements.
77+
assert.dom('[data-test-wizard-hat]').exists();
78+
assert.dom('[data-test-disable-admin-actions]').exists();
79+
assert.dom('[data-test-enable-admin-actions]').doesNotExist();
80+
81+
// Test that the expiry time is sensible. We'll allow a minute either way in
82+
// case of slow tests or slightly wonky clocks.
83+
const disable = find('[data-test-disable-admin-actions] > div');
84+
let seen = 0;
85+
for (const ts of [untilAbout - 60 * 1000, untilAbout, untilAbout + 60 * 1000]) {
86+
const time = format(new Date(ts), 'HH:mm');
87+
if (disable.textContent.includes(time)) {
88+
seen += 1;
89+
}
90+
}
91+
assert.strictEqual(seen, 1);
92+
93+
// Test that the fieldset is not present.
94+
assert.dom('[data-test-placeholder-fieldset]').doesNotExist();
95+
assert.dom('[data-test-version-yank-button="0.1.0"]').exists();
96+
});
97+
98+
test('admin can yank a crate in sudo mode', async function (assert) {
99+
prepare(this, true);
100+
101+
await visit('/crates/foo/versions');
102+
await click('[data-test-enable-admin-actions]');
103+
104+
await click('[data-test-version-yank-button="0.1.0"]');
105+
106+
await waitFor('[data-test-version-unyank-button="0.1.0"]');
107+
assert.dom('[data-test-version-unyank-button="0.1.0"]').exists();
108+
await click('[data-test-version-unyank-button="0.1.0"]');
109+
110+
await waitFor('[data-test-version-yank-button="0.1.0"]');
111+
assert.dom('[data-test-version-yank-button="0.1.0"]').exists();
112+
});
113+
});

0 commit comments

Comments
 (0)