Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
9. Refactoring #1: Moving functional tests out
- Loading branch information
Showing
3 changed files
with
85 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict' | ||
|
||
const { test, trait, before, after } = use('Test/Suite')('Thread') | ||
const { ioc } = use('@adonisjs/fold') | ||
const Thread = use('App/Models/Thread') | ||
const Factory = use('Factory') | ||
|
||
trait('Test/ApiClient') | ||
trait('Auth/Client') | ||
trait('DatabaseTransactions') | ||
|
||
before(() => { | ||
ioc.fake('App/Services/ProfanityGuard', () => { | ||
return { | ||
handle: value => value !== 'jackass' | ||
} | ||
}) | ||
}) | ||
|
||
after(() => { | ||
ioc.restore('App/Services/ProfanityGuard') | ||
}) | ||
|
||
test('authorized user can create threads', async ({ client }) => { | ||
const user = await Factory.model('App/Models/User').create(); | ||
const attributes = { | ||
title: 'test title', | ||
body: 'body', | ||
}; | ||
const response = await client.post('/threads').loginVia(user).send(attributes).end(); | ||
response.assertStatus(200); | ||
const thread = await Thread.firstOrFail(); | ||
response.assertJSON({ thread: thread.toJSON() }); | ||
response.assertJSONSubset({ thread: { ...attributes, user_id: user.id } }); | ||
}) | ||
|
||
test('user can not create thread where title contains profanities', async ({ client }) => { | ||
const user = await Factory.model('App/Models/User').create(); | ||
const attributes = { title: 'jackass', body: 'body' }; | ||
const response = await client.post('/threads').loginVia(user).send(attributes).end(); | ||
response.assertStatus(400); | ||
}) | ||
|
||
test('unauthenticated user cannot create threads', async ({ client }) => { | ||
const response = await client.post('/threads').send({ | ||
title: 'test title', | ||
body: 'body', | ||
}).end(); | ||
response.assertStatus(401); | ||
}) | ||
|
||
test('can not create thread with no body or title', async ({ client }) => { | ||
const user = await Factory.model('App/Models/User').create(); | ||
let response = await client.post('/threads').header('accept', 'application/json').loginVia(user).send({ title: 'test title' }).end(); | ||
response.assertStatus(400); | ||
response.assertJSONSubset([{ message: 'required validation failed on body' }]); | ||
response = await client.post('/threads').header('accept', 'application/json').loginVia(user).send({ body: 'test body' }).end(); | ||
response.assertStatus(400); | ||
response.assertJSONSubset([{ message: 'required validation failed on title' }]); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict' | ||
|
||
const { test, trait, before, after } = use('Test/Suite')('Thread') | ||
const { ioc } = use('@adonisjs/fold') | ||
const Thread = use('App/Models/Thread') | ||
const Factory = use('Factory') | ||
|
||
trait('Test/ApiClient') | ||
trait('Auth/Client') | ||
trait('DatabaseTransactions') | ||
|
||
test('can access single resource', async ({ client }) => { | ||
const thread = await Factory.model('App/Models/Thread').create() | ||
const response = await client.get(thread.url()).send().end() | ||
response.assertStatus(200) | ||
response.assertJSON({ thread: thread.toJSON() }) | ||
}) | ||
|
||
test('can access all resources', async ({ client }) => { | ||
const threads = await Factory.model('App/Models/Thread').createMany(3) | ||
const response = await client.get('threads').send().end() | ||
response.assertStatus(200) | ||
response.assertJSON({ threads: threads.map(thread => thread.toJSON()).sort((a, b) => a.id - b.id) }) | ||
}) |