Skip to content

Commit aafe0bc

Browse files
committed
models/crate: Add setTrustpubOnlyTask
This adds a `setTrustpubOnlyTask` to enable/disable the trusted publishing restriction via the PATCH `/api/v1/crates/{name}` endpoint.
1 parent e9513f5 commit aafe0bc

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

app/models/crate.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { waitForPromise } from '@ember/test-waiters';
44
import { cached } from '@glimmer/tracking';
55

66
import { apiAction } from '@mainmatter/ember-api-actions';
7-
import { task } from 'ember-concurrency';
7+
import { keepLatestTask, task } from 'ember-concurrency';
88

99
export default class Crate extends Model {
1010
@attr name;
@@ -129,4 +129,10 @@ export default class Crate extends Model {
129129
let fut = reload === true ? versionsRef.reload() : versionsRef.load();
130130
return (await fut) ?? [];
131131
});
132+
133+
setTrustpubOnlyTask = keepLatestTask(async trustpubOnly => {
134+
let data = { crate: { trustpub_only: trustpubOnly } };
135+
let payload = await waitForPromise(apiAction(this, { method: 'PATCH', data }));
136+
this.store.pushPayload(payload);
137+
});
132138
}

tests/models/crate-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,52 @@ module('Model | Crate', function (hooks) {
1313
this.store = this.owner.lookup('service:store');
1414
});
1515

16+
module('setTrustpubOnlyTask', function () {
17+
test('enables trustpub_only', async function (assert) {
18+
let user = this.db.user.create();
19+
this.authenticateAs(user);
20+
21+
let crate = this.db.crate.create({ trustpubOnly: false });
22+
this.db.version.create({ crate });
23+
24+
let crateRecord = await this.store.findRecord('crate', crate.name);
25+
assert.false(crateRecord.trustpub_only);
26+
assert.false(this.db.crate.findFirst({ where: { id: { equals: crate.id } } }).trustpubOnly);
27+
28+
await crateRecord.setTrustpubOnlyTask.perform(true);
29+
assert.true(crateRecord.trustpub_only);
30+
assert.true(this.db.crate.findFirst({ where: { id: { equals: crate.id } } }).trustpubOnly);
31+
});
32+
33+
test('disables trustpub_only', async function (assert) {
34+
let user = this.db.user.create();
35+
this.authenticateAs(user);
36+
37+
let crate = this.db.crate.create({ trustpubOnly: true });
38+
this.db.version.create({ crate });
39+
40+
let crateRecord = await this.store.findRecord('crate', crate.name);
41+
assert.true(crateRecord.trustpub_only);
42+
assert.true(this.db.crate.findFirst({ where: { id: { equals: crate.id } } }).trustpubOnly);
43+
44+
await crateRecord.setTrustpubOnlyTask.perform(false);
45+
assert.false(crateRecord.trustpub_only);
46+
assert.false(this.db.crate.findFirst({ where: { id: { equals: crate.id } } }).trustpubOnly);
47+
});
48+
49+
test('requires authentication', async function (assert) {
50+
let crate = this.db.crate.create();
51+
this.db.version.create({ crate });
52+
53+
let crateRecord = await this.store.findRecord('crate', crate.name);
54+
55+
await assert.rejects(crateRecord.setTrustpubOnlyTask.perform(true), function (error) {
56+
assert.deepEqual(error.errors, [{ detail: 'must be logged in to perform that action' }]);
57+
return true;
58+
});
59+
});
60+
});
61+
1662
module('inviteOwner()', function () {
1763
test('happy path', async function (assert) {
1864
let user = this.db.user.create();

0 commit comments

Comments
 (0)