RestrictedResourceController::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Ntb\RestAPI;
4
5
use Config;
6
7
/**
8
 * Class SessionControllerTest
9
 * @author Christian Blank <[email protected]>
10
 */
11
class SessionEndpointTest extends RestTest {
12
13
    public function setUp() {
14
        parent::setUp();
15
        Config::inst()->update('Director', 'rules', [
16
            'v/1/RestrictedRoute/$ID' => 'Ntb\RestAPI\RestrictedResourceController',
17
            'v/1/SessionRoute/$ID' => 'Ntb\RestAPI\SessionController',
18
        ]);
19
        Config::inst()->update('Injector', 'ApiMemberAuthenticator', 'MemberAuthenticator');
20
        Config::inst()->update('BaseRestController', 'Owner', 'Member');
21
    }
22
23
    protected static $fixture_file = [
24
        'silverstripe-rest-api/tests/functional/fixture/Member.yml'
25
    ];
26
27
    public function testTryGetRestrictedResourceWithoutSession() {
28
        $this->makeApiRequest('RestrictedRoute', ['code' => 401]);
29
    }
30
31
    public function testGetRestrictedResource() {
32
        $session = $this->createSession();
33
        $this->makeApiRequest('RestrictedRoute', ['token' => $session['token']]);
34
    }
35
36
    public function testDeleteSession() {
37
        $session = $this->createSession();
38
        $this->makeApiRequest('SessionRoute/me', ['token' => $session['token'], 'method' => 'DELETE']);
39
    }
40
41 View Code Duplication
    public function testTryDeleteSessionWithoutID() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
        $session = $this->createSession();
43
        $result = $this->makeApiRequest('SessionRoute', ['token' => $session['token'], 'method' => 'DELETE', 'code' => 400]);
44
45
        $this->assertTrue(array_key_exists('code', $result));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
        $this->assertTrue(array_key_exists('message', $result));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        $this->assertEquals(404, $result['code']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
    }
49
50 View Code Duplication
    public function testTryDeleteSessionWithWrongId() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
        $session = $this->createSession();
52
        $result = $this->makeApiRequest('SessionRoute/-2', ['token' => $session['token'], 'method' => 'DELETE', 'code' => 400]);
53
54
        $this->assertTrue(array_key_exists('code', $result));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
        $this->assertTrue(array_key_exists('message', $result));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        $this->assertEquals(404, $result['code']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
    }
58
59
    public function testCreateSession() {
60
        $session = $this->createSession();
61
        $this->assertTrue(is_array($session));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
        $this->assertTrue(array_key_exists('user', $session));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
        $this->assertTrue(array_key_exists('token', $session));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
    }
65
66 View Code Duplication
    public function testTryCreateSessionWithWrongPassword() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
        $data = [
68
            'email' => '[email protected]',
69
            'password' => 'wrongPass'
70
        ];
71
72
        $dataString = json_encode($data);
73
        $result = $this->makeApiRequest('SessionRoute', ['body' => $dataString, 'method' => 'POST', 'code' => 401]);
74
75
        $this->assertTrue(array_key_exists('code', $result));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
        $this->assertTrue(array_key_exists('message', $result));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
        $this->assertEquals(401, $result['code']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
    }
79
80 View Code Duplication
    public function testTryCreateSessionWithWrongEmail() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
        $data = [
82
            'email' => 'foo.com',
83
            'password' => 'wrong'
84
        ];
85
86
        $dataString = json_encode($data);
87
        $result = $this->makeApiRequest('SessionRoute', ['body' => $dataString, 'method' => 'POST', 'code' => 422]);
88
89
        $this->assertTrue(array_key_exists('code', $result));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
        $this->assertTrue(array_key_exists('message', $result));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
        $this->assertEquals(422, $result['code']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<Ntb\RestAPI\SessionEndpointTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
    }
93
}
94
95
96
class RestrictedResourceController extends BaseRestController implements \TestOnly {
97
98
    private static $allowed_actions = array (
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
99
        'get' => '->isAuthenticated'
100
    );
101
102
    public function get() {
103
        return ['message' => 'Test GET'];
104
    }
105
}
106