AuthControllerTest.php 2.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php


namespace Tests\Feature\Api\Auth;

use Tests\TestCase;
use Source\Entities\User;
use Source\Entities\Token;
use Source\UseCases\Users\Authenticate\AuthenticateUseCase;

class AuthControllerTest extends TestCase
{
    /**
     * @var \Tests\Feature\Api\Auth\UserAuthenticateUseCaseStub
     */
    protected UserAuthenticateUseCaseStub $useCase;

    public function setUp(): void
    {
        parent::setUp();

        $this->useCase = new UserAuthenticateUseCaseStub();

        $this->app->bind(AuthenticateUseCase::class, function () {
            return $this->useCase;
        });
    }

    /**
     * @test
     */
    public function it_tests_application_login(): void
    {
        $this->useCase->setUserAndToken(
            new User(1, 'Tea', '', '', '', '', '', ''),
            new Token(1, 1, 'token_string')
        );

        $response = $this->postJson('/login', ['Hello' => 'There', 'General', 'Kenobi']);

        $response->assertStatus(200);
        $this->assertEquals(['Hello' => 'There', 'General', 'Kenobi'], $this->useCase->getAttemptedCredentials());
        $response->assertCookie('api_token', 'token_string');
        $response->assertJsonFragment(['first_name' => 'Tea']);
    }

    /**
     * @test
     */
    public function it_tests_saml_redirection(): void
    {
        $this->useCase->setSamlUrl('im dyin bro');
        $response = $this->get('/login');

        $response->assertStatus(302);
        $response->assertRedirect('im dyin bro');
    }

    /**
     * @test
     */
    public function it_handles_saml_callback_when_exception_is_thrown(): void
    {
        $this->useCase->throwCreationException = true;
        $response = $this->get('/handle-login');
        $response->assertStatus(200);
        $response->assertJson(['status' => 'error', 'message' => 'There was an error authenticating the user. Please contact an administrator.']);
    }

    /**
     * @test
     */
    public function it_handles_saml_callback(): void
    {
        $this->useCase->setUserAndToken(
            new User(1, 'Tea', '', '', '', '', '', ''),
            new Token(1, 1, 'token_string')
        );

        $response = $this->get('/handle-login');
        $response->assertCookie('api_token', 'token_string');
        $response->assertStatus(302);
    }

    /**
     * @test
     */
    public function it_logs_the_user_out_and_expires_cookie(): void
    {
        $this->useCase->setSamlUrl('I need to go to work rn tbh');
        $response = $this->withCookie('api_token', 'token')->get('/logout');

        $response->assertStatus(302);
        $response->assertRedirect('I need to go to work rn tbh');
        $response->assertCookieExpired('api_token');
    }
}