Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/crates-io-msw/handlers/crates.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import followCrate from './crates/follow.js';
import following from './crates/following.js';
import getCrate from './crates/get.js';
import listCrates from './crates/list.js';
import patchCrate from './crates/patch.js';
import removeOwners from './crates/remove-owners.js';
import reverseDependencies from './crates/reverse-dependencies.js';
import teamOwners from './crates/team-owners.js';
Expand All @@ -14,6 +15,7 @@ import userOwners from './crates/user-owners.js';
export default [
listCrates,
getCrate,
patchCrate,
deleteCrate,
following,
followCrate,
Expand Down
2 changes: 2 additions & 0 deletions packages/crates-io-msw/handlers/crates/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ test('returns a crate object for known crates', async function () {
num_versions: 1,
repository: null,
recent_downloads: 321,
trustpub_only: false,
updated_at: '2017-02-24T12:34:56Z',
versions: [1],
yanked: false,
Expand Down Expand Up @@ -122,6 +123,7 @@ test('works for non-canonical names', async function () {
num_versions: 1,
repository: null,
recent_downloads: 321,
trustpub_only: false,
updated_at: '2017-02-24T12:34:56Z',
versions: [1],
yanked: false,
Expand Down
1 change: 1 addition & 0 deletions packages/crates-io-msw/handlers/crates/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ test('returns a paginated crates list', async function () {
num_versions: 2,
repository: null,
recent_downloads: 321,
trustpub_only: false,
updated_at: '2017-02-24T12:34:56Z',
versions: null,
yanked: false,
Expand Down
30 changes: 30 additions & 0 deletions packages/crates-io-msw/handlers/crates/patch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { http, HttpResponse } from 'msw';

import { db } from '../../index.js';
import { serializeCrate } from '../../serializers/crate.js';
import { getSession } from '../../utils/session.js';

export default http.patch('/api/v1/crates/:name', async ({ request, params }) => {
let { user } = getSession();
if (!user) {
return HttpResponse.json({ errors: [{ detail: 'must be logged in to perform that action' }] }, { status: 403 });
}

let crate = db.crate.findFirst({ where: { name: { equals: params.name } } });
if (!crate) {
return HttpResponse.json({ errors: [{ detail: `crate \`${params.name}\` does not exist` }] }, { status: 404 });
}

let body = await request.json();

if (body.crate?.trustpub_only != null) {
crate = db.crate.update({
where: { id: { equals: crate.id } },
data: {
trustpubOnly: body.crate.trustpub_only,
},
});
}

return HttpResponse.json({ crate: serializeCrate(crate) });
});
52 changes: 52 additions & 0 deletions packages/crates-io-msw/handlers/crates/patch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { assert, test } from 'vitest';

import { db } from '../../index.js';

test('returns 403 if unauthenticated', async function () {
let response = await fetch('/api/v1/crates/foo', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ crate: { trustpub_only: true } }),
});
assert.strictEqual(response.status, 403);
assert.deepEqual(await response.json(), {
errors: [{ detail: 'must be logged in to perform that action' }],
});
});

test('returns 404 for unknown crates', async function () {
let user = db.user.create();
db.mswSession.create({ user });

let response = await fetch('/api/v1/crates/foo', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ crate: { trustpub_only: true } }),
});
assert.strictEqual(response.status, 404);
assert.deepEqual(await response.json(), { errors: [{ detail: 'crate `foo` does not exist' }] });
});

test('updates trustpub_only flag', async function () {
let user = db.user.create();
db.mswSession.create({ user });

let crate = db.crate.create({ name: 'foo', trustpubOnly: false });
assert.strictEqual(crate.trustpubOnly, false);

db.version.create({ crate, num: '1.0.0' });
db.crateOwnership.create({ crate, user });

let response = await fetch('/api/v1/crates/foo', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ crate: { trustpub_only: true } }),
});
assert.strictEqual(response.status, 200);

let json = await response.json();
assert.strictEqual(json.crate.trustpub_only, true);

let updatedCrate = db.crate.findFirst({ where: { name: { equals: 'foo' } } });
assert.strictEqual(updatedCrate.trustpubOnly, true);
});
4 changes: 4 additions & 0 deletions packages/crates-io-msw/handlers/summary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ test('returns the data for the front page', async function () {
num_versions: 1,
recent_downloads: 321,
repository: null,
trustpub_only: false,
updated_at: '2017-02-24T12:34:56Z',
versions: null,
yanked: false,
Expand Down Expand Up @@ -85,6 +86,7 @@ test('returns the data for the front page', async function () {
num_versions: 1,
repository: null,
recent_downloads: 963,
trustpub_only: false,
updated_at: '2017-02-24T12:34:56Z',
versions: null,
yanked: false,
Expand Down Expand Up @@ -116,6 +118,7 @@ test('returns the data for the front page', async function () {
num_versions: 1,
repository: null,
recent_downloads: 3852,
trustpub_only: false,
updated_at: '2017-02-24T12:34:56Z',
versions: null,
yanked: false,
Expand Down Expand Up @@ -147,6 +150,7 @@ test('returns the data for the front page', async function () {
num_versions: 1,
repository: null,
recent_downloads: 1605,
trustpub_only: false,
updated_at: '2017-02-24T12:34:56Z',
versions: null,
yanked: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ test('happy path', ({ expect }) => {
"name": "crate-1",
"recent_downloads": 321,
"repository": null,
"trustpubOnly": false,
"updated_at": "2017-02-24T12:34:56Z",
Symbol(type): "crate",
Symbol(primaryKey): "id",
Expand Down
2 changes: 2 additions & 0 deletions packages/crates-io-msw/models/crate-ownership.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ test('can set `team`', ({ expect }) => {
"name": "crate-1",
"recent_downloads": 321,
"repository": null,
"trustpubOnly": false,
"updated_at": "2017-02-24T12:34:56Z",
Symbol(type): "crate",
Symbol(primaryKey): "id",
Expand Down Expand Up @@ -88,6 +89,7 @@ test('can set `user`', ({ expect }) => {
"name": "crate-1",
"recent_downloads": 321,
"repository": null,
"trustpubOnly": false,
"updated_at": "2017-02-24T12:34:56Z",
Symbol(type): "crate",
Symbol(primaryKey): "id",
Expand Down
2 changes: 2 additions & 0 deletions packages/crates-io-msw/models/crate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default {
updated_at: String,
badges: Array,
_extra_downloads: Array,
trustpubOnly: Boolean,

categories: manyOf('category'),
keywords: manyOf('keyword'),
Expand All @@ -31,5 +32,6 @@ export default {
applyDefault(attrs, 'repository', () => null);
applyDefault(attrs, 'created_at', () => '2010-06-16T21:30:45Z');
applyDefault(attrs, 'updated_at', () => '2017-02-24T12:34:56Z');
applyDefault(attrs, 'trustpubOnly', () => false);
},
};
2 changes: 2 additions & 0 deletions packages/crates-io-msw/models/crate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test('default are applied', ({ expect }) => {
"name": "crate-1",
"recent_downloads": 321,
"repository": null,
"trustpubOnly": false,
"updated_at": "2017-02-24T12:34:56Z",
Symbol(type): "crate",
Symbol(primaryKey): "id",
Expand Down Expand Up @@ -76,6 +77,7 @@ test('attributes can be set', ({ expect }) => {
"name": "crates-io",
"recent_downloads": 321,
"repository": null,
"trustpubOnly": false,
"updated_at": "2017-02-24T12:34:56Z",
Symbol(type): "crate",
Symbol(primaryKey): "id",
Expand Down
2 changes: 2 additions & 0 deletions packages/crates-io-msw/models/dependency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ test('happy path', ({ expect }) => {
"name": "crate-1",
"recent_downloads": 321,
"repository": null,
"trustpubOnly": false,
"updated_at": "2017-02-24T12:34:56Z",
Symbol(type): "crate",
Symbol(primaryKey): "id",
Expand All @@ -62,6 +63,7 @@ test('happy path', ({ expect }) => {
"name": "crate-2",
"recent_downloads": 1926,
"repository": null,
"trustpubOnly": false,
"updated_at": "2017-02-24T12:34:56Z",
Symbol(type): "crate",
Symbol(primaryKey): "id",
Expand Down
2 changes: 2 additions & 0 deletions packages/crates-io-msw/models/trustpub/gitlab-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test('defaults are applied', ({ expect }) => {
"name": "crate-1",
"recent_downloads": 321,
"repository": null,
"trustpubOnly": false,
"updated_at": "2017-02-24T12:34:56Z",
Symbol(type): "crate",
Symbol(primaryKey): "id",
Expand Down Expand Up @@ -64,6 +65,7 @@ test('fields can be set', ({ expect }) => {
"name": "serde",
"recent_downloads": 321,
"repository": null,
"trustpubOnly": false,
"updated_at": "2017-02-24T12:34:56Z",
Symbol(type): "crate",
Symbol(primaryKey): "id",
Expand Down
1 change: 1 addition & 0 deletions packages/crates-io-msw/models/version-download.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test('happy path', ({ expect }) => {
"name": "crate-1",
"recent_downloads": 321,
"repository": null,
"trustpubOnly": false,
"updated_at": "2017-02-24T12:34:56Z",
Symbol(type): "crate",
Symbol(primaryKey): "id",
Expand Down
1 change: 1 addition & 0 deletions packages/crates-io-msw/models/version.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ test('happy path', ({ expect }) => {
"name": "crate-1",
"recent_downloads": 321,
"repository": null,
"trustpubOnly": false,
"updated_at": "2017-02-24T12:34:56Z",
Symbol(type): "crate",
Symbol(primaryKey): "id",
Expand Down
Loading