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() { |
|
|
|
|
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)); |
|
|
|
|
46
|
|
|
$this->assertTrue(array_key_exists('message', $result)); |
|
|
|
|
47
|
|
|
$this->assertEquals(404, $result['code']); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function testTryDeleteSessionWithWrongId() { |
|
|
|
|
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)); |
|
|
|
|
55
|
|
|
$this->assertTrue(array_key_exists('message', $result)); |
|
|
|
|
56
|
|
|
$this->assertEquals(404, $result['code']); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testCreateSession() { |
60
|
|
|
$session = $this->createSession(); |
61
|
|
|
$this->assertTrue(is_array($session)); |
|
|
|
|
62
|
|
|
$this->assertTrue(array_key_exists('user', $session)); |
|
|
|
|
63
|
|
|
$this->assertTrue(array_key_exists('token', $session)); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function testTryCreateSessionWithWrongPassword() { |
|
|
|
|
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)); |
|
|
|
|
76
|
|
|
$this->assertTrue(array_key_exists('message', $result)); |
|
|
|
|
77
|
|
|
$this->assertEquals(401, $result['code']); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function testTryCreateSessionWithWrongEmail() { |
|
|
|
|
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)); |
|
|
|
|
90
|
|
|
$this->assertTrue(array_key_exists('message', $result)); |
|
|
|
|
91
|
|
|
$this->assertEquals(422, $result['code']); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
class RestrictedResourceController extends BaseRestController implements \TestOnly { |
97
|
|
|
|
98
|
|
|
private static $allowed_actions = array ( |
|
|
|
|
99
|
|
|
'get' => '->isAuthenticated' |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
public function get() { |
103
|
|
|
return ['message' => 'Test GET']; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.