Skip to content

Commit 0f11af4

Browse files
committed
Add tests for GET /api/v1/crates/:id/downloads mirage request handler
1 parent 4f2a797 commit 0f11af4

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

tests/mirage/crates-test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,4 +868,68 @@ module('Mirage | Keywords', function(hooks) {
868868
assert.equal(responsePayload.meta.total, 25);
869869
});
870870
});
871+
872+
module('GET /api/v1/crates/:id/downloads', function() {
873+
test('returns 404 for unknown crates', async function(assert) {
874+
let response = await fetch('/api/v1/crates/foo/downloads');
875+
assert.equal(response.status, 404);
876+
877+
let responsePayload = await response.json();
878+
assert.deepEqual(responsePayload, { errors: [{ detail: 'Not Found' }] });
879+
});
880+
881+
test('empty case', async function(assert) {
882+
this.server.create('crate', { name: 'rand' });
883+
884+
let response = await fetch('/api/v1/crates/rand/downloads');
885+
assert.equal(response.status, 200);
886+
887+
let responsePayload = await response.json();
888+
assert.deepEqual(responsePayload, {
889+
version_downloads: [],
890+
meta: {
891+
extra_downloads: [],
892+
},
893+
});
894+
});
895+
896+
test('returns a list of version downloads belonging to the specified crate version', async function(assert) {
897+
this.server.create('crate', { name: 'rand' });
898+
let versions = this.server.createList('version', 2, { crate: 'rand' });
899+
this.server.create('version-download', { version: versions[0].id, date: '2020-01-13' });
900+
this.server.create('version-download', { version: versions[1].id, date: '2020-01-14' });
901+
this.server.create('version-download', { version: versions[1].id, date: '2020-01-15' });
902+
903+
let response = await fetch('/api/v1/crates/rand/downloads');
904+
assert.equal(response.status, 200);
905+
906+
// TODO Remove the `id` properties from the response
907+
let responsePayload = await response.json();
908+
assert.deepEqual(responsePayload, {
909+
version_downloads: [
910+
{
911+
id: '1',
912+
date: '2020-01-13',
913+
downloads: 9380,
914+
version: '1',
915+
},
916+
{
917+
id: '2',
918+
date: '2020-01-14',
919+
downloads: 16415,
920+
version: '2',
921+
},
922+
{
923+
id: '3',
924+
date: '2020-01-15',
925+
downloads: 23450,
926+
version: '2',
927+
},
928+
],
929+
meta: {
930+
extra_downloads: [],
931+
},
932+
});
933+
});
934+
});
871935
});

0 commit comments

Comments
 (0)