Completed
Push — ezp26179-rest_session_refresh_... ( c14a8b...1fdd78 )
by
unknown
24:14
created

SessionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 8
lcom 2
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testCreateSessionBadCredentials() 0 7 1
A testCreateSession() 0 4 1
A testRefreshSession() 0 5 1
A testRefreshSessionExpired() 0 17 1
A testDeleteSession() 0 4 1
A testDeleteSessionExpired() 0 4 1
A createRefreshRequest() 0 10 1
1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace eZ\Bundle\EzPublishRestBundle\Tests\Functional;
7
8
use Buzz\Message\Response;
9
use stdClass;
10
11
class SessionTest extends TestCase
12
{
13
    public function setUp()
14
    {
15
        $this->autoLogin = false;
16
        parent::setUp();
17
    }
18
19
    public function testCreateSessionBadCredentials()
20
    {
21
        $request = $this->createHttpRequest('POST', '/api/ezp/v2/user/sessions', 'SessionInput+json', 'Session+json');
22
        $request->setContent('{"SessionInput": {"login": "admin", "password": "nopublish"}}');
23
        $response = $this->sendHttpRequest($request);
24
        self::assertHttpResponseCodeEquals($response, 401);
25
    }
26
27
    /**
28
     * @return \stdClass The login request's response
29
     */
30
    public function testCreateSession()
31
    {
32
        return $this->login();
33
    }
34
35
    /**
36
     * @depends testCreateSession
37
     */
38
    public function testRefreshSession(stdClass $session)
39
    {
40
        $response = $this->sendHttpRequest($this->createRefreshRequest($session));
41
        self::assertHttpResponseCodeEquals($response, 200);
42
    }
43
44
    public function testRefreshSessionExpired()
45
    {
46
        $session = $this->login();
47
48
        $deleteRequest = $this->createHttpRequest('DELETE', $session->_href);
49
        $deleteRequest->addHeaders([
50
            sprintf('Cookie: %s=%s', $session->name, $session->identifier),
51
            sprintf('X-CSRF-Token: %s', $session->csrfToken)
52
        ]);
53
        print_r($deleteRequest);
54
        $response = $this->sendHttpRequest($deleteRequest);
55
        self::assertHttpResponseCodeEquals($response, 204);
56
57
        $response = $this->sendHttpRequest($this->createRefreshRequest($session));
58
        self::assertHttpResponseCodeEquals($response, 404);
59
        print_r($response);
60
    }
61
62
    public function testDeleteSession()
63
    {
64
65
    }
66
67
    public function testDeleteSessionExpired()
68
    {
69
70
    }
71
72
    /**
73
     * @param stdClass $session
74
     * @return \Buzz\Message\Request
75
     */
76
    protected function createRefreshRequest(stdClass $session): \Buzz\Message\Request
77
    {
78
        $request = $this->createHttpRequest('POST',
79
            sprintf('/api/ezp/v2/user/sessions/%s/refresh', $session->identifier), '', 'Session+json');
80
        $request->addHeaders([
81
            sprintf('Cookie: %s=%s', $session->name, $session->identifier),
82
            sprintf('X-CSRF-Token: %s', $session->csrfToken)
83
        ]);
84
        return $request;
85
    }
86
}
87