Skip to content

Commit 3a72535

Browse files
flyingluscasvinicius73
authored andcommitted
Login controller test (#11)
* added api base test case * invalid credentials test * login test * fix folder name * composer test script * update readme
1 parent 1e916bd commit 3a72535

File tree

5 files changed

+86
-21
lines changed

5 files changed

+86
-21
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ Make sure you have installed **Node** and [**Yarn**](https://yarnpkg.com/) (late
5555
2. Server side
5656
* API endpoint is http://**localhost:8000/api**
5757

58+
## Testing
59+
60+
Navigate to **webservice** folder and run the composer test script
61+
62+
``` bash
63+
$ composer test
64+
```
65+
5866
## Things worth mentioning
5967

6068
1. Error handling is done globally by making use of Axios' interceptors. But you can still .catch() errors within components to perform actions related to that scope. See /client/src/plugins/http.js;

webservice/composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"autoload-dev": {
3030
"classmap": [
3131
"tests/Support/",
32-
"tests/TestCase.php"
32+
"tests/TestCase.php",
33+
"tests/ApiTestCase.php"
3334
]
3435
},
3536
"scripts": {
@@ -46,7 +47,8 @@
4647
"post-update-cmd": [
4748
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
4849
"php artisan optimize"
49-
]
50+
],
51+
"test": "phpunit"
5052
},
5153
"config": {
5254
"preferred-install": "dist"

webservice/tests/ApiTestCase.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Faker\Factory;
4+
5+
abstract class ApiTestCase extends TestCase
6+
{
7+
/**
8+
* Faker.
9+
*
10+
* @var \Faker\Generator
11+
*/
12+
protected $fake;
13+
14+
public function setUp()
15+
{
16+
parent::setUp();
17+
18+
$this->fake = Factory::create();
19+
}
20+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
use App\User;
4+
use Illuminate\Support\Facades\Hash;
5+
use Illuminate\Foundation\Testing\WithoutMiddleware;
6+
use Illuminate\Foundation\Testing\DatabaseMigrations;
7+
use Illuminate\Foundation\Testing\DatabaseTransactions;
8+
9+
class LoginControllerTest extends ApiTestCase
10+
{
11+
use DatabaseMigrations, Factory;
12+
13+
/**
14+
* @test
15+
*/
16+
public function is_checking_for_invalid_credentials()
17+
{
18+
$this->json('POST', '/api/login', [
19+
'email' => '[email protected]',
20+
'password' => 'dummypassword',
21+
]);
22+
23+
$this->assertResponseStatus(401);
24+
$this->seeJsonStructure([
25+
'messages' => [[]],
26+
]);
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function can_get_an_authenticated_token()
33+
{
34+
$email = '[email protected]';
35+
$password = 'dummypassword';
36+
37+
$this->create(User::class, [
38+
'email' => $email,
39+
'password' => Hash::make($password),
40+
]);
41+
42+
$this->json('POST', '/api/login', [
43+
'email' => $email,
44+
'password' => $password,
45+
]);
46+
47+
$this->assertResponseOk();
48+
$this->seeJsonStructure([
49+
'token', 'user' => [
50+
'id', 'name', 'email',
51+
],
52+
]);
53+
}
54+
}

webservice/tests/ExampleTest.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)