Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
Doorcode
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
5
Issues
5
List
Boards
Labels
Service Desk
Milestones
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Guardians of the Kretschmar Elock System
Doorcode
Commits
fad50f4a
Commit
fad50f4a
authored
Mar 04, 2020
by
Jacob Priddy
👌
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add the rest of the authenticate tests
parent
b49c6662
Pipeline
#2606
canceled with stages
in 34 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
208 additions
and
17 deletions
+208
-17
src/web/backend/src/Entities/Token.php
src/web/backend/src/Entities/Token.php
+27
-3
src/web/backend/src/Gateways/Saml/InMemorySamlRepository.php
src/web/backend/src/Gateways/Saml/InMemorySamlRepository.php
+3
-3
src/web/backend/src/Gateways/Tokens/InMemoryTokensRepository.php
.../backend/src/Gateways/Tokens/InMemoryTokensRepository.php
+5
-3
src/web/backend/src/UseCases/Users/Authenticate/Authenticate.php
.../backend/src/UseCases/Users/Authenticate/Authenticate.php
+8
-8
src/web/backend/tests/Unit/Source/UseCases/Users/Authenticate/SamlUseCaseTest.php
...it/Source/UseCases/Users/Authenticate/SamlUseCaseTest.php
+165
-0
No files found.
src/web/backend/src/Entities/Token.php
View file @
fad50f4a
...
...
@@ -124,7 +124,7 @@ class Token
}
/**
* @return Carbon|null
* @return
\Carbon\
Carbon|null
*/
public
function
getExpiresAt
():
?Carbon
{
...
...
@@ -132,7 +132,7 @@ class Token
}
/**
* @return Carbon|null
* @return
\Carbon\
Carbon|null
*/
public
function
getCreatedAt
():
?Carbon
{
...
...
@@ -140,13 +140,21 @@ class Token
}
/**
* @return Carbon|null
* @return
\Carbon\
Carbon|null
*/
public
function
getUpdatedAt
():
?Carbon
{
return
$this
->
updatedAt
;
}
/**
* @param \Carbon\Carbon|null $expires
*/
public
function
setExpiresAt
(
?Carbon
$expires
):
void
{
$this
->
expiresAt
=
$expires
;
}
/**
* @param Carbon $date
* @return bool
...
...
@@ -155,4 +163,20 @@ class Token
{
return
$this
->
expiresAt
===
null
||
$this
->
expiresAt
->
isAfter
(
$date
);
}
/**
* @return bool
*/
public
function
isValid
():
bool
{
return
$this
->
isValidAtTime
(
Carbon
::
now
());
}
/**
* @return bool
*/
public
function
isInvalid
():
bool
{
return
!
$this
->
isValid
();
}
}
src/web/backend/src/Gateways/Saml/InMemorySamlRepository.php
View file @
fad50f4a
...
...
@@ -7,9 +7,9 @@ use Source\Entities\SamlUser;
class
InMemorySamlRepository
implements
SamlRepository
{
protected
?SamlUser
$userToLogInAs
;
protected
?SamlUser
$userToLogInAs
=
null
;
protected
?SamlUser
$loggedInUser
;
protected
?SamlUser
$loggedInUser
=
null
;
protected
string
$loginUrl
;
...
...
@@ -23,7 +23,7 @@ class InMemorySamlRepository implements SamlRepository
$this
->
logoutUrl
=
$logoutUrl
;
}
public
function
setLoginUser
(
SamlUser
$user
):
void
public
function
setLoginUser
(
?
SamlUser
$user
):
void
{
$this
->
userToLogInAs
=
$user
;
}
...
...
src/web/backend/src/Gateways/Tokens/InMemoryTokensRepository.php
View file @
fad50f4a
...
...
@@ -50,8 +50,10 @@ class InMemoryTokensRepository implements TokensRepository
*/
public
function
invalidateToken
(
string
$token
):
void
{
$this
->
tokens
=
array_filter
(
$this
->
tokens
,
static
function
(
Token
$t
)
use
(
$token
)
{
return
!
$t
->
matches
(
$token
);
});
$tok
=
$this
->
findValidToken
(
$token
);
if
(
$tok
)
{
$tok
->
setExpiresAt
(
Carbon
::
now
());
}
}
}
src/web/backend/src/UseCases/Users/Authenticate/Authenticate.php
View file @
fad50f4a
...
...
@@ -86,25 +86,25 @@ class Authenticate implements AuthenticateUseCase
*/
public
function
handleSamlLogin
(
Presenter
$presenter
):
void
{
$
u
ser
=
$this
->
saml
->
handleLogin
();
$
samlU
ser
=
$this
->
saml
->
handleLogin
();
if
(
!
$
u
ser
)
{
if
(
!
$
samlU
ser
)
{
throw
new
UserCreationException
();
}
// First check to see if the user exists in the database.
$user
=
$this
->
users
->
findByEmail
(
$
u
ser
->
getEmail
());
$user
=
$this
->
users
->
findByEmail
(
$
samlU
ser
->
getEmail
());
// If the user does not exist, create them.
if
(
!
$user
)
{
$user
=
$this
->
users
->
create
(
new
User
(
0
,
$
u
ser
->
getFirstName
(),
$
u
ser
->
getLastName
(),
$
u
ser
->
getDisplayName
(),
$
u
ser
->
getEmplid
(),
$
u
ser
->
getEmail
(),
$
samlU
ser
->
getFirstName
(),
$
samlU
ser
->
getLastName
(),
$
samlU
ser
->
getDisplayName
(),
$
samlU
ser
->
getEmplid
(),
$
samlU
ser
->
getEmail
(),
null
,
null
)
...
...
src/web/backend/tests/Unit/Source/UseCases/Users/Authenticate/SamlUseCaseTest.php
0 → 100644
View file @
fad50f4a
<?php
namespace
Tests\Unit\Source\UseCases\Users\Authenticate
;
use
Carbon\Carbon
;
use
Source\Entities\User
;
use
Source\Entities\Token
;
use
Source\Entities\SamlUser
;
use
Tests\Doubles\InMemoryUsersRepositoryStub
;
use
Source\UseCases\Users\Authenticate\Authenticate
;
use
Source\UseCases\Users\Authenticate\UserCreationException
;
class
SamlUseCaseTest
extends
UseCaseBaseTest
{
protected
const
VALID_EMAIL
=
'email'
;
/**
* @param \Source\Entities\SamlUser|null $samlUser
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
protected
function
handleLoginTest
(
?SamlUser
$samlUser
=
null
):
void
{
$this
->
saml
->
setLoginUser
(
$samlUser
);
$this
->
saml
->
login
();
$this
->
useCase
->
handleSamlLogin
(
$this
->
presenter
);
$this
->
response
=
$this
->
presenter
->
response
;
}
/**
* @return \Source\Entities\User
*/
protected
function
createUser
():
User
{
return
$this
->
users
->
create
(
new
User
(
0
,
''
,
''
,
''
,
''
,
self
::
VALID_EMAIL
,
''
,
''
));
}
/**
* @return \Source\Entities\SamlUser
*/
protected
function
createSamlUser
():
SamlUser
{
return
new
SamlUser
(
'first'
,
'last'
,
'emplid'
,
strtoupper
(
self
::
VALID_EMAIL
));
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
public
function
it_calls_present_on_presenter
():
void
{
$samlUser
=
$this
->
createSamlUser
();
$this
->
handleLoginTest
(
$samlUser
);
$this
->
assertTrue
(
$this
->
presenter
->
wasPresenterCalled
());
}
/**
* @test
*/
public
function
it_returns_saml_login_link
():
void
{
$link
=
$this
->
useCase
->
handToSaml
();
$this
->
assertEquals
(
$this
->
loginUrl
,
$link
);
}
/**
* @test
*/
public
function
it_returns_saml_logout_link
():
void
{
$link
=
$this
->
useCase
->
samlLogout
(
null
);
$this
->
assertEquals
(
$this
->
logoutUrl
,
$link
);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public
function
it_invalidates_token_on_saml_logout
():
void
{
$user
=
$this
->
createUser
();
$this
->
tokens
->
create
(
new
Token
(
0
,
$user
->
getId
(),
'token'
));
$this
->
useCase
->
samlLogout
(
'token'
);
$tok
=
$this
->
tokens
->
all
()[
0
];
$this
->
assertTrue
(
$tok
->
isInvalid
());
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
public
function
it_cannot_create_null_users
():
void
{
$this
->
expectException
(
UserCreationException
::
class
);
$this
->
handleLoginTest
();
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
public
function
it_creates_a_user_if_they_do_not_exist_in_local_database
():
void
{
$samlUser
=
$this
->
createSamlUser
();
$this
->
handleLoginTest
(
$samlUser
);
$this
->
assertCount
(
1
,
$this
->
users
->
all
());
$user
=
$this
->
users
->
all
()[
0
];
$this
->
assertEquals
(
'First'
,
$user
->
getFirstName
());
$this
->
assertEquals
(
'Last'
,
$user
->
getLastName
());
$this
->
assertEquals
(
'First Last'
,
$user
->
getDisplayName
());
$this
->
assertEquals
(
'emplid'
,
$user
->
getEmplid
());
$this
->
assertEquals
(
'email'
,
$user
->
getEmail
());
$this
->
assertNull
(
$user
->
getPassword
());
$this
->
assertNull
(
$user
->
getDoorcode
());
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
public
function
it_throws_an_exception_if_it_cannot_create_a_user
():
void
{
$samlUser
=
$this
->
createSamlUser
();
$users
=
new
InMemoryUsersRepositoryStub
();
$this
->
useCase
=
new
Authenticate
(
$users
,
$this
->
saml
,
$this
->
tokens
);
$this
->
expectException
(
UserCreationException
::
class
);
$this
->
handleLoginTest
(
$samlUser
);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
public
function
it_creates_a_token_for_the_user
():
void
{
$user
=
$this
->
createUser
();
$samlUser
=
$this
->
createSamlUser
();
$this
->
handleLoginTest
(
$samlUser
);
$this
->
assertCount
(
1
,
$this
->
tokens
->
all
());
$token
=
$this
->
tokens
->
all
()[
0
];
$this
->
assertLessThan
(
Carbon
::
now
()
->
addDays
(
2
),
$token
->
getExpiresAt
());
$this
->
assertGreaterThan
(
Carbon
::
now
()
->
addDays
(
1
),
$token
->
getExpiresAt
());
$this
->
assertEquals
(
$user
->
getId
(),
$token
->
getUserId
());
$this
->
assertEquals
(
60
,
strlen
(
$token
->
getTokenString
()));
$this
->
assertEquals
(
$token
,
$this
->
response
->
getToken
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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