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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
<?php
/*
* Copyright (c) 2017 Hubzilla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Zotlabs\Tests\Unit\Access;
use Zotlabs\Tests\Unit\UnitTestCase;
use Zotlabs\Access\AccessList;
/**
* @brief Unit Test case for AccessList class.
*
* @covers Zotlabs\Access\AccessList
*/
class AccessListTest extends UnitTestCase {
/**
* Expected result for most tests.
*/
protected array $expectedResult = [
'allow_cid' => '<acid><acid2>',
'allow_gid' => '<agid>',
'deny_cid' => '',
'deny_gid' => '<dgid><dgid2>'
];
public function testConstructor() {
$channel = [
'channel_allow_cid' => '<acid><acid2>',
'channel_allow_gid' => '<agid>',
'channel_deny_cid' => '',
'channel_deny_gid' => '<dgid><dgid2>'
];
$accessList = new AccessList($channel);
$this->assertEquals($this->expectedResult, $accessList->get());
$this->assertFalse($accessList->get_explicit());
}
/**
* AccessList constructor should throw an exception if input is not
* an array.
*/
public function testConstructorThrowsOnInvalidInputType() {
$this->expectException("TypeError");
$accessList = new AccessList('invalid');
}
/**
* AccessList constructor should throw an exception on
* invalid input.
*/
public function testConstructorThrowsOnMissingKeysInArray() {
$this->expectException("Exception");
$this->expectExceptionMessage("Invalid AccessList object");
$accessList = new AccessList(['something_else' => 'should_this_fail?']);
}
/**
* Test that the defaults are as expected when constructed with
* an empty array.
*/
public function testDefaults() {
$accessList = new AccessList([]);
$this->assertFalse($accessList->get_explicit());
$this->assertFalse($accessList->is_private());
$expected = [
'allow_cid' => '',
'allow_gid' => '',
'deny_cid' => '',
'deny_gid' => ''
];
$this->assertEquals($expected, $accessList->get());
}
public function testSet() {
$arr = [
'allow_cid' => '<acid><acid2>',
'allow_gid' => '<agid>',
'deny_cid' => '',
'deny_gid' => '<dgid><dgid2>'
];
$accessList = new AccessList([]);
// default explicit true
$accessList->set($arr);
$this->assertEquals($this->expectedResult, $accessList->get());
$this->assertTrue($accessList->get_explicit());
// set explicit false
$accessList->set($arr, false);
$this->assertEquals($this->expectedResult, $accessList->get());
$this->assertFalse($accessList->get_explicit());
}
/**
* The set method should throw an exception if input is not an array.
*/
public function testSetThrowsOnInvalidInputType() {
$this->expectException('TypeError');
$accessList = new AccessList([]);
$accessList->set('invalid');
}
public function testSetThrowsOnMissingKeysInArray() {
$this->expectException('Exception');
$this->expectExceptionMessage('Invalid AccessList object');
$accessList = new AccessList([]);
$accessList->set(['something_else' => 'should_this_fail?']);
}
/**
* The set_from_array() function calls some other functions, too which are
* not yet unit tested.
*
* @uses ::perms2str
* @uses ::sanitise_acl
* @uses ::notags
*/
public function testSetFromArray() {
// array
$arraySetFromArray = [
'contact_allow' => ['acid', 'acid2'],
'group_allow' => ['agid'],
'contact_deny' => [],
'group_deny' => ['dgid', 'dgid2']
];
$accessList = new AccessList([]);
$accessList->set_from_array($arraySetFromArray);
$this->assertEquals($this->expectedResult, $accessList->get());
$this->assertTrue($accessList->get_explicit());
// string
$stringSetFromArray = [
'contact_allow' => 'acid,acid2',
'group_allow' => 'agid',
'contact_deny' => '',
'group_deny' => 'dgid, dgid2'
];
$accessList2 = new AccessList([]);
$accessList2->set_from_array($stringSetFromArray, false);
$this->assertEquals($this->expectedResult, $accessList2->get());
$this->assertFalse($accessList2->get_explicit());
}
/**
* The AccessList should be private if any of the fields are set,
*
* @dataProvider isprivateProvider
*/
public function testIsPrivate($channel) {
$accessListPrivate = new AccessList($channel);
$this->assertTrue($accessListPrivate->is_private());
}
public static function isprivateProvider() {
return [
'all set' => [[
'channel_allow_cid' => '<acid>',
'channel_allow_gid' => '<agid>',
'channel_deny_cid' => '<dcid>',
'channel_deny_gid' => '<dgid>'
]],
'only allow_cid set' => [[
'channel_allow_cid' => '<acid>',
'channel_allow_gid' => '',
'channel_deny_cid' => '',
'channel_deny_gid' => ''
]],
'only allow_gid set' => [[
'channel_allow_cid' => '',
'channel_allow_gid' => '<agid>',
'channel_deny_cid' => '',
'channel_deny_gid' => ''
]],
'only deny_cid set' => [[
'channel_allow_cid' => '',
'channel_allow_gid' => '',
'channel_deny_cid' => '<dcid>',
'channel_deny_gid' => ''
]],
'only deny_gid set' => [[
'channel_allow_cid' => '',
'channel_allow_gid' => '',
'channel_deny_cid' => '',
'channel_deny_gid' => '<dgid>'
]],
'acid+null' => [[
'channel_allow_cid' => '<acid>',
'channel_allow_gid' => null,
'channel_deny_cid' => '',
'channel_deny_gid' => ''
]]
];
}
}
|