-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a42624c
commit 0a40ee5
Showing
18 changed files
with
591 additions
and
18 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
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,10 @@ | ||
HOST=127.0.0.1 | ||
PORT=4000 | ||
NODE_ENV=testing | ||
|
||
DB_CONNECTION=mysql | ||
DB_HOST=127.0.0.1 | ||
DB_PORT=3306 | ||
DB_USER=root | ||
DB_PASSWORD= | ||
DB_DATABASE=api.lausanne-esports.ch-test |
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,23 @@ | ||
'use strict' | ||
|
||
const User = use('App/Models/User') | ||
const InvalidCredential = use('App/Exceptions/InvalidCredentialException') | ||
|
||
class SessionController { | ||
async store ({ auth, request, response }) { | ||
const { email, password } = request.all() | ||
|
||
const token = await auth.attempt(email, password) | ||
.catch((e) => { | ||
throw new InvalidCredential('Authentication failed. Either supplied credentials are invalid or the account is inactive', 401, 'E_INVALID_CREDENTIAL') | ||
}) | ||
|
||
return response.ok({ | ||
token, | ||
status: 200, | ||
message: 'Logged in successfully', | ||
}) | ||
} | ||
} | ||
|
||
module.exports = SessionController |
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,23 @@ | ||
'use strict' | ||
|
||
const User = use('App/Models/User') | ||
|
||
class UserController { | ||
async store ({ request, response }) { | ||
const data = request.only([ | ||
'username', 'email', 'password', 'password_confirmation'] | ||
) | ||
|
||
delete data.password_confirmation | ||
|
||
const user = await User.create(data) | ||
|
||
return response.ok({ | ||
user, | ||
status: 200, | ||
message: 'Account created successfully', | ||
}) | ||
} | ||
} | ||
|
||
module.exports = UserController |
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,45 @@ | ||
'use strict' | ||
|
||
/** | ||
* This class handles all exceptions thrown during | ||
* the HTTP request lifecycle. | ||
* | ||
* @class ExceptionHandler | ||
*/ | ||
class ExceptionHandler { | ||
/** | ||
* Handle exception thrown during the HTTP lifecycle | ||
* | ||
* @method handle | ||
* | ||
* @param {Object} error | ||
* @param {Object} options.request | ||
* @param {Object} options.response | ||
* | ||
* @return {void} | ||
*/ | ||
async handle (error, { request, response }) { | ||
response.status(error.status).send({ | ||
errors: [{ | ||
"status": error.status, | ||
"code": error.code, | ||
"detail": error.message, | ||
}] | ||
}) | ||
} | ||
|
||
/** | ||
* Report exception for logging or debugging. | ||
* | ||
* @method report | ||
* | ||
* @param {Object} error | ||
* @param {Object} options.request | ||
* | ||
* @return {void} | ||
*/ | ||
async report (error, { request }) { | ||
} | ||
} | ||
|
||
module.exports = ExceptionHandler |
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,12 @@ | ||
'use strict' | ||
|
||
const { LogicalException } = require('@adonisjs/generic-exceptions') | ||
|
||
class InvalidCredentialException extends LogicalException { | ||
/** | ||
* Handle this exception by itself | ||
*/ | ||
// handle () {} | ||
} | ||
|
||
module.exports = InvalidCredentialException |
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,15 @@ | ||
'use strict' | ||
|
||
class AcceptFormatGate { | ||
async handle ({ request }, next) { | ||
// console.log(request.request.headers.accept) | ||
// if (!request.accepts(['html', 'json']) === 'json') { | ||
request.request.headers.accept = 'application/json' | ||
// } | ||
|
||
// call next to advance the request | ||
await next() | ||
} | ||
} | ||
|
||
module.exports = AcceptFormatGate |
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,20 @@ | ||
'use strict' | ||
|
||
class SessionStore { | ||
get formatter () { | ||
return 'jsonapi' | ||
} | ||
|
||
get validateAll () { | ||
return true | ||
} | ||
|
||
get rules () { | ||
return { | ||
email: 'required|email', | ||
password: 'required', | ||
} | ||
} | ||
} | ||
|
||
module.exports = SessionStore |
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,22 @@ | ||
'use strict' | ||
|
||
class UserStore { | ||
get formatter () { | ||
return 'jsonapi' | ||
} | ||
|
||
get validateAll () { | ||
return true | ||
} | ||
|
||
get rules () { | ||
return { | ||
username: 'required|unique:users', | ||
email: 'required|email|unique:users', | ||
password: 'required', | ||
password_confirmation: 'required_if:password|same:password', | ||
} | ||
} | ||
} | ||
|
||
module.exports = UserStore |
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
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
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,130 @@ | ||
'use strict' | ||
|
||
const Factory = use('Factory') | ||
const { test, trait } = use('Test/Suite')('Session Store') | ||
|
||
trait('Test/ApiClient') | ||
trait('DatabaseTransactions') | ||
|
||
test('should be able to signin', async ({ assert, client }) => { | ||
const user = await Factory.model('App/Models/User').create() | ||
|
||
const response = await client | ||
.post('sessions') | ||
.send({ | ||
email: user.email, | ||
password: 'secret', | ||
}) | ||
.end() | ||
|
||
response.assertStatus(200) | ||
response.assertJSONSubset({ | ||
message: 'Logged in successfully', | ||
}) | ||
}) | ||
|
||
test('should throw invalid credential when email is incorrect', async ({ assert, client }) => { | ||
const response = await client | ||
.post('sessions') | ||
.send({ | ||
email: 'romain.lanz@lausanne-esports.ch', | ||
password: 'secret', | ||
}) | ||
.end() | ||
|
||
response.assertStatus(401) | ||
response.assertJSONSubset({ | ||
errors: [{ | ||
status: 401, | ||
code: 'E_INVALID_CREDENTIAL', | ||
detail: 'E_INVALID_CREDENTIAL: Authentication failed. Either supplied credentials are invalid or the account is inactive', | ||
}] | ||
}) | ||
}) | ||
|
||
test('should throw invalid credential when password is incorrect', async ({ assert, client }) => { | ||
const user = await Factory.model('App/Models/User').create() | ||
|
||
const response = await client | ||
.post('sessions') | ||
.send({ | ||
email: user.email, | ||
password: 'ThisIsntGoingToWork', | ||
}) | ||
.end() | ||
|
||
response.assertStatus(401) | ||
response.assertJSONSubset({ | ||
errors: [{ | ||
status: 401, | ||
code: 'E_INVALID_CREDENTIAL', | ||
detail: 'E_INVALID_CREDENTIAL: Authentication failed. Either supplied credentials are invalid or the account is inactive', | ||
}] | ||
}) | ||
}) | ||
|
||
test('should test that email is required', async ({ assert, client }) => { | ||
const response = await client | ||
.post('sessions') | ||
.send({ | ||
password: 'ThisIsntGoingToWork' | ||
}) | ||
.end() | ||
|
||
response.assertStatus(400) | ||
response.assertJSONSubset({ | ||
errors: [{ | ||
source: { pointer: 'email' }, | ||
title: 'required', | ||
}] | ||
}) | ||
}) | ||
|
||
test('should test that password is required', async ({ assert, client }) => { | ||
const response = await client | ||
.post('sessions') | ||
.send({ | ||
email: 'romain.lanz@lausanne-esports.ch', | ||
}) | ||
.end() | ||
|
||
response.assertStatus(400) | ||
response.assertJSONSubset({ | ||
errors: [{ | ||
source: { pointer: 'password' }, | ||
title: 'required', | ||
}] | ||
}) | ||
}) | ||
|
||
test('should test that email must be correctly formated', async ({ assert, client }) => { | ||
const response = await client | ||
.post('sessions') | ||
.send({ | ||
email: 'ThisIsntGoingToWork' | ||
}) | ||
.end() | ||
|
||
response.assertStatus(400) | ||
response.assertJSONSubset({ | ||
errors: [{ | ||
source: { pointer: 'email' }, | ||
title: 'email', | ||
}] | ||
}) | ||
}) | ||
|
||
test('should test that all errors are sent back', async ({ assert, client }) => { | ||
const response = await client | ||
.post('sessions') | ||
.send({}) | ||
.end() | ||
|
||
response.assertStatus(400) | ||
response.assertJSONSubset({ | ||
errors: [ | ||
{ source: { pointer: 'email' }, title: 'required' }, | ||
{ source: { pointer: 'password' }, title: 'required' }, | ||
] | ||
}) | ||
}) |
Oops, something went wrong.