Skip to content

Commit c3d8a76

Browse files
authored
Merge pull request #12382 from Turbo87/trustpub-only-msw
msw/crate: Add `trustpub_only` support
2 parents 584d849 + 331246c commit c3d8a76

File tree

14 files changed

+104
-0
lines changed

14 files changed

+104
-0
lines changed

packages/crates-io-msw/handlers/crates.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import followCrate from './crates/follow.js';
55
import following from './crates/following.js';
66
import getCrate from './crates/get.js';
77
import listCrates from './crates/list.js';
8+
import patchCrate from './crates/patch.js';
89
import removeOwners from './crates/remove-owners.js';
910
import reverseDependencies from './crates/reverse-dependencies.js';
1011
import teamOwners from './crates/team-owners.js';
@@ -14,6 +15,7 @@ import userOwners from './crates/user-owners.js';
1415
export default [
1516
listCrates,
1617
getCrate,
18+
patchCrate,
1719
deleteCrate,
1820
following,
1921
followCrate,

packages/crates-io-msw/handlers/crates/get.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ test('returns a crate object for known crates', async function () {
4141
num_versions: 1,
4242
repository: null,
4343
recent_downloads: 321,
44+
trustpub_only: false,
4445
updated_at: '2017-02-24T12:34:56Z',
4546
versions: [1],
4647
yanked: false,
@@ -122,6 +123,7 @@ test('works for non-canonical names', async function () {
122123
num_versions: 1,
123124
repository: null,
124125
recent_downloads: 321,
126+
trustpub_only: false,
125127
updated_at: '2017-02-24T12:34:56Z',
126128
versions: [1],
127129
yanked: false,

packages/crates-io-msw/handlers/crates/list.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ test('returns a paginated crates list', async function () {
5858
num_versions: 2,
5959
repository: null,
6060
recent_downloads: 321,
61+
trustpub_only: false,
6162
updated_at: '2017-02-24T12:34:56Z',
6263
versions: null,
6364
yanked: false,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { http, HttpResponse } from 'msw';
2+
3+
import { db } from '../../index.js';
4+
import { serializeCrate } from '../../serializers/crate.js';
5+
import { getSession } from '../../utils/session.js';
6+
7+
export default http.patch('/api/v1/crates/:name', async ({ request, params }) => {
8+
let { user } = getSession();
9+
if (!user) {
10+
return HttpResponse.json({ errors: [{ detail: 'must be logged in to perform that action' }] }, { status: 403 });
11+
}
12+
13+
let crate = db.crate.findFirst({ where: { name: { equals: params.name } } });
14+
if (!crate) {
15+
return HttpResponse.json({ errors: [{ detail: `crate \`${params.name}\` does not exist` }] }, { status: 404 });
16+
}
17+
18+
let body = await request.json();
19+
20+
if (body.crate?.trustpub_only != null) {
21+
crate = db.crate.update({
22+
where: { id: { equals: crate.id } },
23+
data: {
24+
trustpubOnly: body.crate.trustpub_only,
25+
},
26+
});
27+
}
28+
29+
return HttpResponse.json({ crate: serializeCrate(crate) });
30+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { assert, test } from 'vitest';
2+
3+
import { db } from '../../index.js';
4+
5+
test('returns 403 if unauthenticated', async function () {
6+
let response = await fetch('/api/v1/crates/foo', {
7+
method: 'PATCH',
8+
headers: { 'Content-Type': 'application/json' },
9+
body: JSON.stringify({ crate: { trustpub_only: true } }),
10+
});
11+
assert.strictEqual(response.status, 403);
12+
assert.deepEqual(await response.json(), {
13+
errors: [{ detail: 'must be logged in to perform that action' }],
14+
});
15+
});
16+
17+
test('returns 404 for unknown crates', async function () {
18+
let user = db.user.create();
19+
db.mswSession.create({ user });
20+
21+
let response = await fetch('/api/v1/crates/foo', {
22+
method: 'PATCH',
23+
headers: { 'Content-Type': 'application/json' },
24+
body: JSON.stringify({ crate: { trustpub_only: true } }),
25+
});
26+
assert.strictEqual(response.status, 404);
27+
assert.deepEqual(await response.json(), { errors: [{ detail: 'crate `foo` does not exist' }] });
28+
});
29+
30+
test('updates trustpub_only flag', async function () {
31+
let user = db.user.create();
32+
db.mswSession.create({ user });
33+
34+
let crate = db.crate.create({ name: 'foo', trustpubOnly: false });
35+
assert.strictEqual(crate.trustpubOnly, false);
36+
37+
db.version.create({ crate, num: '1.0.0' });
38+
db.crateOwnership.create({ crate, user });
39+
40+
let response = await fetch('/api/v1/crates/foo', {
41+
method: 'PATCH',
42+
headers: { 'Content-Type': 'application/json' },
43+
body: JSON.stringify({ crate: { trustpub_only: true } }),
44+
});
45+
assert.strictEqual(response.status, 200);
46+
47+
let json = await response.json();
48+
assert.strictEqual(json.crate.trustpub_only, true);
49+
50+
let updatedCrate = db.crate.findFirst({ where: { name: { equals: 'foo' } } });
51+
assert.strictEqual(updatedCrate.trustpubOnly, true);
52+
});

packages/crates-io-msw/handlers/summary.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ test('returns the data for the front page', async function () {
5454
num_versions: 1,
5555
recent_downloads: 321,
5656
repository: null,
57+
trustpub_only: false,
5758
updated_at: '2017-02-24T12:34:56Z',
5859
versions: null,
5960
yanked: false,
@@ -85,6 +86,7 @@ test('returns the data for the front page', async function () {
8586
num_versions: 1,
8687
repository: null,
8788
recent_downloads: 963,
89+
trustpub_only: false,
8890
updated_at: '2017-02-24T12:34:56Z',
8991
versions: null,
9092
yanked: false,
@@ -116,6 +118,7 @@ test('returns the data for the front page', async function () {
116118
num_versions: 1,
117119
repository: null,
118120
recent_downloads: 3852,
121+
trustpub_only: false,
119122
updated_at: '2017-02-24T12:34:56Z',
120123
versions: null,
121124
yanked: false,
@@ -147,6 +150,7 @@ test('returns the data for the front page', async function () {
147150
num_versions: 1,
148151
repository: null,
149152
recent_downloads: 1605,
153+
trustpub_only: false,
150154
updated_at: '2017-02-24T12:34:56Z',
151155
versions: null,
152156
yanked: false,

packages/crates-io-msw/models/crate-owner-invitation.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ test('happy path', ({ expect }) => {
4747
"name": "crate-1",
4848
"recent_downloads": 321,
4949
"repository": null,
50+
"trustpubOnly": false,
5051
"updated_at": "2017-02-24T12:34:56Z",
5152
Symbol(type): "crate",
5253
Symbol(primaryKey): "id",

packages/crates-io-msw/models/crate-ownership.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ test('can set `team`', ({ expect }) => {
4545
"name": "crate-1",
4646
"recent_downloads": 321,
4747
"repository": null,
48+
"trustpubOnly": false,
4849
"updated_at": "2017-02-24T12:34:56Z",
4950
Symbol(type): "crate",
5051
Symbol(primaryKey): "id",
@@ -88,6 +89,7 @@ test('can set `user`', ({ expect }) => {
8889
"name": "crate-1",
8990
"recent_downloads": 321,
9091
"repository": null,
92+
"trustpubOnly": false,
9193
"updated_at": "2017-02-24T12:34:56Z",
9294
Symbol(type): "crate",
9395
Symbol(primaryKey): "id",

packages/crates-io-msw/models/crate.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default {
1616
updated_at: String,
1717
badges: Array,
1818
_extra_downloads: Array,
19+
trustpubOnly: Boolean,
1920

2021
categories: manyOf('category'),
2122
keywords: manyOf('keyword'),
@@ -31,5 +32,6 @@ export default {
3132
applyDefault(attrs, 'repository', () => null);
3233
applyDefault(attrs, 'created_at', () => '2010-06-16T21:30:45Z');
3334
applyDefault(attrs, 'updated_at', () => '2017-02-24T12:34:56Z');
35+
applyDefault(attrs, 'trustpubOnly', () => false);
3436
},
3537
};

packages/crates-io-msw/models/crate.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ test('default are applied', ({ expect }) => {
1919
"name": "crate-1",
2020
"recent_downloads": 321,
2121
"repository": null,
22+
"trustpubOnly": false,
2223
"updated_at": "2017-02-24T12:34:56Z",
2324
Symbol(type): "crate",
2425
Symbol(primaryKey): "id",
@@ -76,6 +77,7 @@ test('attributes can be set', ({ expect }) => {
7677
"name": "crates-io",
7778
"recent_downloads": 321,
7879
"repository": null,
80+
"trustpubOnly": false,
7981
"updated_at": "2017-02-24T12:34:56Z",
8082
Symbol(type): "crate",
8183
Symbol(primaryKey): "id",

0 commit comments

Comments
 (0)