Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Guardians of the Kretschmar Elock System
Doorcode
Commits
8e3b920b
Commit
8e3b920b
authored
May 18, 2020
by
Jacob Priddy
👌
Browse files
add command authorizer test
parent
d16541d1
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/web/backend/src/Authorization/ApiAuthorizer.php
View file @
8e3b920b
...
...
@@ -32,6 +32,11 @@ class ApiAuthorizer implements Authorizer
*/
protected
?string
$currentUserId
;
/**
* @var string[]
*/
protected
array
$userGroups
;
public
function
__construct
(
?string
$currentUserId
,
UsersRepository
$users
,
...
...
@@ -49,7 +54,10 @@ class ApiAuthorizer implements Authorizer
*/
public
function
setCurrentUserId
(
?string
$id
):
void
{
$this
->
currentUserId
=
$id
;
if
(
$this
->
currentUserId
!==
$id
)
{
$this
->
userGroups
=
[];
$this
->
currentUserId
=
$id
;
}
}
/**
...
...
@@ -114,7 +122,11 @@ class ApiAuthorizer implements Authorizer
*/
protected
function
getGroupsForUser
(
string
$userId
):
array
{
return
static
::
groupNames
(
$this
->
groupUserRepository
->
getGroupsForUser
(
$userId
));
if
(
!
$this
->
userGroups
)
{
$this
->
userGroups
=
static
::
groupNames
(
$this
->
groupUserRepository
->
getGroupsForUser
(
$userId
));
}
return
$this
->
userGroups
;
}
/**
...
...
src/web/backend/tests/Doubles/CommandHandlerStub.php
0 → 100644
View file @
8e3b920b
<?php
namespace
Tests\Doubles
;
use
Source\Entities\User
;
use
Source\UseCases\Door\Commands\CommandHandler
;
class
CommandHandlerStub
implements
CommandHandler
{
public
bool
$handleReturn
=
true
;
/**
* @inheritDoc
*/
public
function
handle
(
User
$user
,
string
$doorId
,
string
$commandString
):
bool
{
return
$this
->
handleReturn
;
}
}
src/web/backend/tests/Unit/Source/UseCases/Door/Access/Authorizers/CommandAuthorizerTest.php
0 → 100644
View file @
8e3b920b
<?php
namespace
Tests\Unit\Source\UseCases\Door\Access\Authorizers
;
use
Carbon\Carbon
;
use
Source\Entities\User
;
use
PHPUnit\Framework\TestCase
;
use
Tests\Doubles\AuthorizerStub
;
use
Tests\Doubles\CommandHandlerStub
;
use
Source\UseCases\Door\Access\AccessAuthorizer
;
use
Source\UseCases\Door\Access\Authorizers\CommandAuthorizer
;
class
CommandAuthorizerTest
extends
TestCase
{
/**
* @var \Tests\Doubles\AuthorizerStub
*/
protected
AuthorizerStub
$auth
;
/**
* @var \Tests\Doubles\CommandHandlerStub
*/
protected
CommandHandlerStub
$handler
;
/**
* @var \Source\UseCases\Door\Access\Authorizers\CommandAuthorizer
*/
protected
CommandAuthorizer
$authorizer
;
protected
int
$status
;
public
function
setUp
():
void
{
parent
::
setUp
();
$this
->
auth
=
new
AuthorizerStub
();
$this
->
handler
=
new
CommandHandlerStub
();
$this
->
authorizer
=
new
CommandAuthorizer
(
$this
->
auth
,
$this
->
handler
);
}
protected
function
handleTest
(
bool
$commandSuccess
,
?User
$user
,
?string
$command
=
''
):
void
{
$this
->
handler
->
handleReturn
=
$commandSuccess
;
$this
->
status
=
$this
->
authorizer
->
check
(
$user
,
Carbon
::
now
(),
''
,
''
,
$command
);
}
protected
function
createUser
():
User
{
return
new
User
(
0
,
''
,
''
,
''
,
''
);
}
/**
* @test
*/
public
function
it_does_nothing_when_command_is_not_set
():
void
{
$this
->
handleTest
(
true
,
$this
->
createUser
(),
null
);
$this
->
assertEquals
(
AccessAuthorizer
::
CONTINUE
,
$this
->
status
);
}
/**
* @test
*/
public
function
it_does_nothing_when_user_is_not_set
():
void
{
$this
->
handleTest
(
true
,
null
,
''
);
$this
->
assertEquals
(
AccessAuthorizer
::
CONTINUE
,
$this
->
status
);
}
/**
* @test
*/
public
function
it_denies_if_user_is_not_authorized
():
void
{
$this
->
auth
->
setNext
(
false
);
$this
->
handleTest
(
true
,
$this
->
createUser
(),
'command'
);
$this
->
assertEquals
(
AccessAuthorizer
::
DENY
,
$this
->
status
);
}
/**
* @test
*/
public
function
it_denies_if_command_fails
():
void
{
$this
->
handleTest
(
false
,
$this
->
createUser
(),
'command'
);
$this
->
assertEquals
(
AccessAuthorizer
::
DENY
,
$this
->
status
);
}
/**
* @test
*/
public
function
it_allows_if_command_succeeds
():
void
{
$this
->
handleTest
(
true
,
$this
->
createUser
(),
'command'
);
$this
->
assertEquals
(
AccessAuthorizer
::
ALLOW
,
$this
->
status
);
}
/**
* @test
*/
public
function
it_does_nothing_if_command_is_empty
():
void
{
$this
->
handleTest
(
true
,
$this
->
createUser
(),
''
);
$this
->
assertEquals
(
AccessAuthorizer
::
CONTINUE
,
$this
->
status
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment