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
68 changes: 50 additions & 18 deletions mirage/route-handlers/crates.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,38 +167,53 @@ export function register(server) {
return resp;
});

server.get('/api/v1/crates/:name/:version_num/authors', (schema, request) => {
server.get('/api/v1/crates/:name/:version/authors', (schema, request) => {
let { name } = request.params;
let crate = schema.crates.findBy({ name });
if (!crate) return notFound();

let num = request.params.version_num;
let num = request.params.version;
let version = schema.versions.findBy({ crateId: crate.id, num });
if (!version) return { errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${num}\`` }] };
if (!version)
return new Response(
404,
{},
{ errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${num}\`` }] },
);

return { meta: { names: [] }, users: [] };
});

server.get('/api/v1/crates/:name/:version_num/dependencies', (schema, request) => {
server.get('/api/v1/crates/:name/:version/dependencies', (schema, request) => {
let { name } = request.params;
let crate = schema.crates.findBy({ name });
if (!crate) return notFound();

let num = request.params.version_num;
let num = request.params.version;
let version = schema.versions.findBy({ crateId: crate.id, num });
if (!version) return { errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${num}\`` }] };
if (!version)
return new Response(
404,
{},
{ errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${num}\`` }] },
);

return schema.dependencies.where({ versionId: version.id });
});

server.get('/api/v1/crates/:name/:version_num/downloads', function (schema, request) {
server.get('/api/v1/crates/:name/:version/downloads', function (schema, request) {
let { name } = request.params;
let crate = schema.crates.findBy({ name });
if (!crate) return notFound();

let versionNum = request.params.version_num;
let version = schema.versions.findBy({ crateId: crate.id, num: versionNum });
if (!version) return { errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${versionNum}\`` }] };
let num = request.params.version;
let version = schema.versions.findBy({ crateId: crate.id, num });
if (!version)
return new Response(
404,
{},
{ errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${num}\`` }] },
);

return schema.versionDownloads.where({ versionId: version.id });
});
Expand Down Expand Up @@ -339,19 +354,36 @@ export function register(server) {
return { ok: true, msg: 'owners successfully removed' };
});

server.get('/api/v1/crates/:name/:version', function (schema, request) {
let { name } = request.params;
let crate = schema.crates.findBy({ name });
if (!crate) return notFound();

let num = request.params.version;
let version = schema.versions.findBy({ crateId: crate.id, num });
if (!version) {
return new Response(
404,
{},
{ errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${num}\`` }] },
);
}
return this.serialize(version);
});

server.patch('/api/v1/crates/:name/:version', function (schema, request) {
let { user } = getSession(schema);
if (!user) {
return new Response(403, {}, { errors: [{ detail: 'must be logged in to perform that action' }] });
}

const { name, version: versionNum } = request.params;
const { name, version: num } = request.params;
const crate = schema.crates.findBy({ name });
if (!crate) {
return notFound();
}

const version = schema.versions.findBy({ crateId: crate.id, num: versionNum });
const version = schema.versions.findBy({ crateId: crate.id, num });
if (!version) {
return notFound();
}
Expand All @@ -371,13 +403,13 @@ export function register(server) {
return new Response(403, {}, { errors: [{ detail: 'must be logged in to perform that action' }] });
}

const { name, version: versionNum } = request.params;
const { name, version: num } = request.params;
const crate = schema.crates.findBy({ name });
if (!crate) {
return notFound();
}

const version = schema.versions.findBy({ crateId: crate.id, num: versionNum });
const version = schema.versions.findBy({ crateId: crate.id, num });
if (!version) {
return notFound();
}
Expand All @@ -393,13 +425,13 @@ export function register(server) {
return new Response(403, {}, { errors: [{ detail: 'must be logged in to perform that action' }] });
}

const { name, version: versionNum } = request.params;
const { name, version: num } = request.params;
const crate = schema.crates.findBy({ name });
if (!crate) {
return notFound();
}

const version = schema.versions.findBy({ crateId: crate.id, num: versionNum });
const version = schema.versions.findBy({ crateId: crate.id, num });
if (!version) {
return notFound();
}
Expand All @@ -410,13 +442,13 @@ export function register(server) {
});

server.get('/api/v1/crates/:name/:version/readme', (schema, request) => {
const { name, version: versionNum } = request.params;
const { name, version: num } = request.params;
const crate = schema.crates.findBy({ name });
if (!crate) {
return new Response(403, { 'Content-Type': 'text/html' }, '');
}

const version = schema.versions.findBy({ crateId: crate.id, num: versionNum });
const version = schema.versions.findBy({ crateId: crate.id, num });
if (!version || !version.readme) {
return new Response(403, { 'Content-Type': 'text/html' }, '');
}
Expand Down
8 changes: 3 additions & 5 deletions tests/mirage/crates/versions/authors-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fetch from 'fetch';
import { setupTest } from '../../../helpers';
import setupMirage from '../../../helpers/setup-mirage';

module('Mirage | GET /api/v1/crates/:id/:version/authors', function (hooks) {
module('Mirage | GET /api/v1/crates/:name/:version/authors', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

Expand All @@ -15,13 +15,11 @@ module('Mirage | GET /api/v1/crates/:id/:version/authors', function (hooks) {
assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] });
});

test('returns 200 for unknown versions', async function (assert) {
test('returns 404 for unknown versions', async function (assert) {
this.server.create('crate', { name: 'rand' });

let response = await fetch('/api/v1/crates/rand/1.0.0/authors');
// we should probably return 404 for this, but the production API
// currently doesn't do this either
assert.strictEqual(response.status, 200);
assert.strictEqual(response.status, 404);
assert.deepEqual(await response.json(), { errors: [{ detail: 'crate `rand` does not have a version `1.0.0`' }] });
});

Expand Down
8 changes: 3 additions & 5 deletions tests/mirage/crates/versions/dependencies-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fetch from 'fetch';
import { setupTest } from '../../../helpers';
import setupMirage from '../../../helpers/setup-mirage';

module('Mirage | GET /api/v1/crates/:id/:version/dependencies', function (hooks) {
module('Mirage | GET /api/v1/crates/:name/:version/dependencies', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

Expand All @@ -15,13 +15,11 @@ module('Mirage | GET /api/v1/crates/:id/:version/dependencies', function (hooks)
assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] });
});

test('returns 200 for unknown versions', async function (assert) {
test('returns 404 for unknown versions', async function (assert) {
this.server.create('crate', { name: 'rand' });

let response = await fetch('/api/v1/crates/rand/1.0.0/dependencies');
// we should probably return 404 for this, but the production API
// currently doesn't do this either
assert.strictEqual(response.status, 200);
assert.strictEqual(response.status, 404);
assert.deepEqual(await response.json(), { errors: [{ detail: 'crate `rand` does not have a version `1.0.0`' }] });
});

Expand Down
8 changes: 3 additions & 5 deletions tests/mirage/crates/versions/downloads-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fetch from 'fetch';
import { setupTest } from '../../../helpers';
import setupMirage from '../../../helpers/setup-mirage';

module('Mirage | GET /api/v1/crates/:id/:version/downloads', function (hooks) {
module('Mirage | GET /api/v1/crates/:name/:version/downloads', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

Expand All @@ -15,13 +15,11 @@ module('Mirage | GET /api/v1/crates/:id/:version/downloads', function (hooks) {
assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] });
});

test('returns 200 for unknown versions', async function (assert) {
test('returns 404 for unknown versions', async function (assert) {
this.server.create('crate', { name: 'rand' });

let response = await fetch('/api/v1/crates/rand/1.0.0/downloads');
// we should probably return 404 for this, but the production API
// currently doesn't do this either
assert.strictEqual(response.status, 200);
assert.strictEqual(response.status, 404);
assert.deepEqual(await response.json(), { errors: [{ detail: 'crate `rand` does not have a version `1.0.0`' }] });
});

Expand Down
57 changes: 57 additions & 0 deletions tests/mirage/crates/versions/get-by-num-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { module, test } from 'qunit';

import fetch from 'fetch';

import { setupTest } from '../../../helpers';
import setupMirage from '../../../helpers/setup-mirage';

module('Mirage | GET /api/v1/crates/:name/:version', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

test('returns 404 for unknown crates', async function (assert) {
let response = await fetch('/api/v1/crates/foo/1.0.0-beta.1');
assert.strictEqual(response.status, 404);
assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] });
});

test('returns 404 for unknown versions', async function (assert) {
let crate = this.server.create('crate', { name: 'rand' });
this.server.create('version', { crate, num: '1.0.0-alpha.1' });
let response = await fetch('/api/v1/crates/rand/1.0.0-beta.1');
assert.strictEqual(response.status, 404);
assert.deepEqual(await response.json(), {
errors: [{ detail: 'crate `rand` does not have a version `1.0.0-beta.1`' }],
});
});

test('returns a version object for known version', async function (assert) {
let crate = this.server.create('crate', { name: 'rand' });
this.server.create('version', { crate, num: '1.0.0-beta.1' });

let response = await fetch('/api/v1/crates/rand/1.0.0-beta.1');
assert.strictEqual(response.status, 200);
assert.deepEqual(await response.json(), {
version: {
crate: 'rand',
crate_size: 0,
created_at: '2010-06-16T21:30:45Z',
dl_path: '/api/v1/crates/rand/1.0.0-beta.1/download',
downloads: 0,
id: '1',
license: 'MIT/Apache-2.0',
links: {
dependencies: '/api/v1/crates/rand/1.0.0-beta.1/dependencies',
version_downloads: '/api/v1/crates/rand/1.0.0-beta.1/downloads',
},
num: '1.0.0-beta.1',
published_by: null,
readme_path: '/api/v1/crates/rand/1.0.0-beta.1/readme',
rust_version: null,
updated_at: '2017-02-24T12:34:56Z',
yank_message: null,
yanked: false,
},
});
});
});
2 changes: 1 addition & 1 deletion tests/mirage/crates/versions/list-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fetch from 'fetch';
import { setupTest } from '../../../helpers';
import setupMirage from '../../../helpers/setup-mirage';

module('Mirage | GET /api/v1/crates/:id/versions', function (hooks) {
module('Mirage | GET /api/v1/crates/:name/versions', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

Expand Down
2 changes: 1 addition & 1 deletion tests/mirage/crates/versions/patch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const UNYANK_BODY = JSON.stringify({
},
});

module('Mirage | PATCH /api/v1/crates/:crate/:version', function (hooks) {
module('Mirage | PATCH /api/v1/crates/:name/:version', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

Expand Down
2 changes: 1 addition & 1 deletion tests/mirage/crates/versions/readme-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fetch from 'fetch';
import { setupTest } from '../../../helpers';
import setupMirage from '../../../helpers/setup-mirage';

module('Mirage | GET /api/v1/crates/:id/:version/readme', function (hooks) {
module('Mirage | GET /api/v1/crates/:name/:version/readme', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

Expand Down
2 changes: 1 addition & 1 deletion tests/mirage/crates/versions/yank/unyank-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fetch from 'fetch';
import { setupTest } from '../../../../helpers';
import setupMirage from '../../../../helpers/setup-mirage';

module('Mirage | PUT /api/v1/crates/:crateId/unyank', function (hooks) {
module('Mirage | PUT /api/v1/crates/:name/unyank', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

Expand Down
2 changes: 1 addition & 1 deletion tests/mirage/crates/versions/yank/yank-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fetch from 'fetch';
import { setupTest } from '../../../../helpers';
import setupMirage from '../../../../helpers/setup-mirage';

module('Mirage | DELETE /api/v1/crates/:crateId/yank', function (hooks) {
module('Mirage | DELETE /api/v1/crates/:name/yank', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

Expand Down
Loading