Passed
Push — master ( 436c58...98654e )
by Anton
06:21 queued 03:52
created

AuthCycleTest::testLogin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 8
c 1
b 1
f 1
dl 0
loc 15
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Framework\Http;
13
14
use Cycle\ORM\ORMInterface;
15
use Spiral\Auth\Cycle\Token;
16
use Spiral\Encrypter\EncrypterFactory;
17
use Spiral\Framework\HttpTest;
18
use Spiral\Http\Http;
19
20
class AuthCycleTest extends HttpTest
21
{
22
    public function setUp(): void
23
    {
24
        $this->app = $this->makeApp();
25
        $key = $this->app->get(EncrypterFactory::class)->generateKey();
26
27
        $this->app = $this->makeApp([
28
            'ENCRYPTER_KEY' => $key,
29
            'CYCLE_AUTH'    => true
30
        ]);
31
32
        $this->app->console()->run('cycle:sync');
33
34
        $this->http = $this->app->get(Http::class);
35
    }
36
37
    public function testNoToken(): void
38
    {
39
        $this->assertSame(
40
            'none',
41
            (string)$this->get('/auth/token')->getBody()
42
        );
43
    }
44
45
    public function testLogin(): void
46
    {
47
        $result = $this->get('/auth/login');
48
49
        $this->assertSame('OK', (string)$result->getBody());
50
51
        $cookies = $this->fetchCookies($result->getHeader('Set-Cookie'));
52
        $this->assertTrue(isset($cookies['token']));
53
54
        $token = $this->app->get(ORMInterface::class)->getRepository(Token::class)->findOne();
55
        $this->assertSame(['userID' => 1], $token->getPayload());
56
57
        $result = $this->get('/auth/token', [], [], $cookies);
58
59
        $this->assertNotSame('none', (string)$result->getBody());
60
    }
61
}
62