Skip to content

Commit

Permalink
9. Refactoring #1: Moving functional tests out
Browse files Browse the repository at this point in the history
  • Loading branch information
MZanggl committed Dec 8, 2019
1 parent 358466c commit ec1ebfe
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 55 deletions.
60 changes: 60 additions & 0 deletions test/functional/create-thread.spec.js
@@ -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' }]);
})
Expand Up @@ -21,36 +21,6 @@ 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('unauthenticated user cannot delete threads', async ({ assert, client }) => {
const thread = await Factory.model('App/Models/Thread').create()
const response = await client.delete(thread.url()).send().end()
Expand Down Expand Up @@ -108,23 +78,13 @@ test('authorized user can update title and body of threads', async ({ assert, cl

test('moderator can update title and body of threads', async ({ assert, client }) => {
const thread = await Factory.model('App/Models/Thread').create()
const moderator = await Factory.model('App/Models/User').create({ type: 1})
const moderator = await Factory.model('App/Models/User').create({ type: 1 })
const attributes = { title: 'new title', body: 'new body' }

const response = await client.put(thread.url()).loginVia(moderator).send(attributes).end()
response.assertStatus(200)
})

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' }])
})

test('can not update thread with no body or title', async ({ client }) => {
const thread = await Factory.model('App/Models/Thread').create()
const user = await thread.user().first()
Expand All @@ -137,17 +97,3 @@ test('can not update thread with no body or title', async ({ client }) => {
response.assertStatus(400)
response.assertJSONSubset([{ message: 'required validation failed on title' }])
})

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) })
})
24 changes: 24 additions & 0 deletions test/functional/read-thread.spec.js
@@ -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) })
})

0 comments on commit ec1ebfe

Please sign in to comment.