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
101 changes: 59 additions & 42 deletions packages/crates-io-msw/handlers/api-tokens/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ test('creates a new API token', async function () {
let token = db.apiToken.findMany(null)[0];
expect(token).toBeTruthy();

expect(await response.json()).toEqual({
api_token: {
id: 1,
crate_scopes: null,
created_at: '2017-11-20T11:23:45.000Z',
endpoint_scopes: null,
expired_at: null,
last_used_at: null,
name: 'foooo',
revoked: false,
token: token.token,
},
});
expect(await response.json()).toMatchInlineSnapshot(`
{
"api_token": {
"crate_scopes": null,
"created_at": "2017-11-20T11:23:45.000Z",
"endpoint_scopes": null,
"expired_at": null,
"id": 1,
"last_used_at": null,
"name": "foooo",
"revoked": false,
"token": "6270739405881613",
},
}
`);
});

test('creates a new API token with scopes', async function () {
Expand All @@ -54,19 +56,26 @@ test('creates a new API token with scopes', async function () {
let token = db.apiToken.findMany(null)[0];
expect(token).toBeTruthy();

expect(await response.json()).toEqual({
api_token: {
id: 1,
crate_scopes: ['serde', 'serde-*'],
created_at: '2017-11-20T11:23:45.000Z',
endpoint_scopes: ['publish-update'],
expired_at: null,
last_used_at: null,
name: 'foooo',
revoked: false,
token: token.token,
},
});
expect(await response.json()).toMatchInlineSnapshot(`
{
"api_token": {
"crate_scopes": [
"serde",
"serde-*",
],
"created_at": "2017-11-20T11:23:45.000Z",
"endpoint_scopes": [
"publish-update",
],
"expired_at": null,
"id": 1,
"last_used_at": null,
"name": "foooo",
"revoked": false,
"token": "6270739405881613",
},
}
`);
});

test('creates a new API token with expiry date', async function () {
Expand All @@ -85,26 +94,34 @@ test('creates a new API token with expiry date', async function () {
let token = db.apiToken.findMany(null)[0];
expect(token).toBeTruthy();

expect(await response.json()).toEqual({
api_token: {
id: 1,
crate_scopes: null,
created_at: '2017-11-20T11:23:45.000Z',
endpoint_scopes: null,
expired_at: '2023-12-24T12:34:56.000Z',
last_used_at: null,
name: 'foooo',
revoked: false,
token: token.token,
},
});
expect(await response.json()).toMatchInlineSnapshot(`
{
"api_token": {
"crate_scopes": null,
"created_at": "2017-11-20T11:23:45.000Z",
"endpoint_scopes": null,
"expired_at": "2023-12-24T12:34:56.000Z",
"id": 1,
"last_used_at": null,
"name": "foooo",
"revoked": false,
"token": "6270739405881613",
},
}
`);
});

test('returns an error if unauthenticated', async function () {
let body = JSON.stringify({ api_token: {} });
let response = await fetch('/api/v1/me/tokens', { method: 'PUT', body });
expect(response.status).toBe(403);
expect(await response.json()).toEqual({
errors: [{ detail: 'must be logged in to perform that action' }],
});
expect(await response.json()).toMatchInlineSnapshot(`
{
"errors": [
{
"detail": "must be logged in to perform that action",
},
],
}
`);
});
14 changes: 10 additions & 4 deletions packages/crates-io-msw/handlers/api-tokens/delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('revokes an API token', async function () {

let response = await fetch(`/api/v1/me/tokens/${token.id}`, { method: 'DELETE' });
expect(response.status).toBe(200);
expect(await response.json()).toEqual({});
expect(await response.json()).toMatchInlineSnapshot(`{}`);

let tokens = db.apiToken.findMany(null);
expect(tokens.length).toBe(0);
Expand All @@ -22,7 +22,13 @@ test('returns an error if unauthenticated', async function () {

let response = await fetch(`/api/v1/me/tokens/${token.id}`, { method: 'DELETE' });
expect(response.status).toBe(403);
expect(await response.json()).toEqual({
errors: [{ detail: 'must be logged in to perform that action' }],
});
expect(await response.json()).toMatchInlineSnapshot(`
{
"errors": [
{
"detail": "must be logged in to perform that action",
},
],
}
`);
});
51 changes: 36 additions & 15 deletions packages/crates-io-msw/handlers/api-tokens/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ test('returns the requested token', async function () {

let response = await fetch(`/api/v1/me/tokens/${token.id}`);
expect(response.status).toBe(200);
expect(await response.json()).toEqual({
api_token: {
id: 1,
crate_scopes: ['serde', 'serde-*'],
created_at: '2017-11-19T17:59:22.000Z',
endpoint_scopes: ['publish-update'],
expired_at: null,
last_used_at: null,
name: 'API Token 1',
},
});
expect(await response.json()).toMatchInlineSnapshot(`
{
"api_token": {
"crate_scopes": [
"serde",
"serde-*",
],
"created_at": "2017-11-19T17:59:22.000Z",
"endpoint_scopes": [
"publish-update",
],
"expired_at": null,
"id": 1,
"last_used_at": null,
"name": "API Token 1",
},
}
`);
});

test('returns 404 if token not found', async function () {
Expand All @@ -33,13 +40,27 @@ test('returns 404 if token not found', async function () {

let response = await fetch('/api/v1/me/tokens/42');
expect(response.status).toBe(404);
expect(await response.json()).toEqual({ errors: [{ detail: 'Not Found' }] });
expect(await response.json()).toMatchInlineSnapshot(`
{
"errors": [
{
"detail": "Not Found",
},
],
}
`);
});

test('returns an error if unauthenticated', async function () {
let response = await fetch('/api/v1/me/tokens/42');
expect(response.status).toBe(403);
expect(await response.json()).toEqual({
errors: [{ detail: 'must be logged in to perform that action' }],
});
expect(await response.json()).toMatchInlineSnapshot(`
{
"errors": [
{
"detail": "must be logged in to perform that action",
},
],
}
`);
});
87 changes: 52 additions & 35 deletions packages/crates-io-msw/handlers/api-tokens/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,44 @@ test('returns the list of API token for the authenticated `user`', async functio

let response = await fetch('/api/v1/me/tokens');
expect(response.status).toBe(200);
expect(await response.json()).toEqual({
api_tokens: [
{
id: 3,
crate_scopes: null,
created_at: '2017-11-19T14:59:22.000Z',
endpoint_scopes: null,
expired_at: null,
last_used_at: null,
name: 'API Token 3',
},
{
id: 2,
crate_scopes: null,
created_at: '2017-11-19T13:59:22.000Z',
endpoint_scopes: null,
expired_at: '2023-11-20T10:59:22.000Z',
last_used_at: null,
name: 'API Token 2',
},
{
id: 1,
crate_scopes: ['serde', 'serde-*'],
created_at: '2017-11-19T12:59:22.000Z',
endpoint_scopes: ['publish-update'],
expired_at: null,
last_used_at: null,
name: 'API Token 1',
},
],
});
expect(await response.json()).toMatchInlineSnapshot(`
{
"api_tokens": [
{
"crate_scopes": null,
"created_at": "2017-11-19T14:59:22.000Z",
"endpoint_scopes": null,
"expired_at": null,
"id": 3,
"last_used_at": null,
"name": "API Token 3",
},
{
"crate_scopes": null,
"created_at": "2017-11-19T13:59:22.000Z",
"endpoint_scopes": null,
"expired_at": "2023-11-20T10:59:22.000Z",
"id": 2,
"last_used_at": null,
"name": "API Token 2",
},
{
"crate_scopes": [
"serde",
"serde-*",
],
"created_at": "2017-11-19T12:59:22.000Z",
"endpoint_scopes": [
"publish-update",
],
"expired_at": null,
"id": 1,
"last_used_at": null,
"name": "API Token 1",
},
],
}
`);
});

test('empty list case', async function () {
Expand All @@ -66,13 +73,23 @@ test('empty list case', async function () {

let response = await fetch('/api/v1/me/tokens');
expect(response.status).toBe(200);
expect(await response.json()).toEqual({ api_tokens: [] });
expect(await response.json()).toMatchInlineSnapshot(`
{
"api_tokens": [],
}
`);
});

test('returns an error if unauthenticated', async function () {
let response = await fetch('/api/v1/me/tokens');
expect(response.status).toBe(403);
expect(await response.json()).toEqual({
errors: [{ detail: 'must be logged in to perform that action' }],
});
expect(await response.json()).toMatchInlineSnapshot(`
{
"errors": [
{
"detail": "must be logged in to perform that action",
},
],
}
`);
});
54 changes: 33 additions & 21 deletions packages/crates-io-msw/handlers/categories/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import { db } from '../../index.js';
test('returns 404 for unknown categories', async function () {
let response = await fetch('/api/v1/categories/foo');
expect(response.status).toBe(404);
expect(await response.json()).toEqual({ errors: [{ detail: 'Not Found' }] });
expect(await response.json()).toMatchInlineSnapshot(`
{
"errors": [
{
"detail": "Not Found",
},
],
}
`);
});

test('returns a category object for known categories', async function () {
Expand All @@ -16,16 +24,18 @@ test('returns a category object for known categories', async function () {

let response = await fetch('/api/v1/categories/no-std');
expect(response.status).toBe(200);
expect(await response.json()).toEqual({
category: {
id: 'no-std',
category: 'no-std',
crates_cnt: 0,
created_at: '2010-06-16T21:30:45Z',
description: 'Crates that are able to function without the Rust standard library.',
slug: 'no-std',
},
});
expect(await response.json()).toMatchInlineSnapshot(`
{
"category": {
"category": "no-std",
"crates_cnt": 0,
"created_at": "2010-06-16T21:30:45Z",
"description": "Crates that are able to function without the Rust standard library.",
"id": "no-std",
"slug": "no-std",
},
}
`);
});

test('calculates `crates_cnt` correctly', async function () {
Expand All @@ -36,14 +46,16 @@ test('calculates `crates_cnt` correctly', async function () {

let response = await fetch('/api/v1/categories/test-cli-category');
expect(response.status).toBe(200);
expect(await response.json()).toEqual({
category: {
category: 'test-cli-category',
crates_cnt: 7,
created_at: '2010-06-16T21:30:45Z',
description: 'This is the description for the category called "test-cli-category"',
id: 'test-cli-category',
slug: 'test-cli-category',
},
});
expect(await response.json()).toMatchInlineSnapshot(`
{
"category": {
"category": "test-cli-category",
"crates_cnt": 7,
"created_at": "2010-06-16T21:30:45Z",
"description": "This is the description for the category called "test-cli-category"",
"id": "test-cli-category",
"slug": "test-cli-category",
},
}
`);
});
Loading
Loading