Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
administration
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OZG-Cloud
app
administration
Commits
368f266f
Commit
368f266f
authored
1 year ago
by
Lukas Malte Monnerjahn
Browse files
Options
Downloads
Patches
Plain Diff
OZG-5176 restructure CurrentUserHelperTest
parent
7e9146b6
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/test/java/de/ozgcloud/admin/common/user/CurrentUserHelperTest.java
+127
-43
127 additions, 43 deletions
.../de/ozgcloud/admin/common/user/CurrentUserHelperTest.java
with
127 additions
and
43 deletions
src/test/java/de/ozgcloud/admin/common/user/CurrentUserHelperTest.java
+
127
−
43
View file @
368f266f
...
...
@@ -23,7 +23,6 @@
package
de.ozgcloud.admin.common.user
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Nested
;
import
org.junit.jupiter.api.Test
;
...
...
@@ -40,9 +39,120 @@ import org.springframework.security.core.userdetails.User;
import
java.util.Collection
;
import
java.util.List
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.*;
class
CurrentUserHelperTest
{
@DisplayName
(
"Has role"
)
@Nested
class
TestHasRole
{
@Mock
private
final
Authentication
mockAuthentication
=
Mockito
.
mock
(
Authentication
.
class
);
@Mock
private
final
User
mockPrincipal
=
Mockito
.
mock
(
User
.
class
);
@Test
void
shouldNotHaveRoleIfNull
()
{
try
(
MockedStatic
<
CurrentUserHelper
>
mockUserHelper
=
Mockito
.
mockStatic
(
CurrentUserHelper
.
class
,
Mockito
.
CALLS_REAL_METHODS
)
)
{
mockUserHelper
.
when
(
CurrentUserHelper:
:
getAuthentication
).
thenReturn
(
null
);
boolean
hasRole
=
CurrentUserHelper
.
hasRole
(
UserRole
.
ADMIN_USER
);
assertThat
(
hasRole
).
isFalse
();
}
}
@Test
void
shouldNotHaveRoleIfPrincipalIsNull
()
{
Mockito
.
when
(
mockAuthentication
.
getPrincipal
()).
thenReturn
(
null
);
try
(
MockedStatic
<
CurrentUserHelper
>
mockUserHelper
=
Mockito
.
mockStatic
(
CurrentUserHelper
.
class
,
Mockito
.
CALLS_REAL_METHODS
)
)
{
mockUserHelper
.
when
(
CurrentUserHelper:
:
getAuthentication
).
thenReturn
(
mockAuthentication
);
boolean
hasRole
=
CurrentUserHelper
.
hasRole
(
UserRole
.
ADMIN_USER
);
assertThat
(
hasRole
).
isFalse
();
}
}
@Test
void
shouldNotHaveRole
()
{
Mockito
.
when
(
mockAuthentication
.
getPrincipal
()).
thenReturn
(
mockPrincipal
);
List
<
GrantedAuthority
>
authorities
=
List
.
of
();
Mockito
.<
Collection
<?
extends
GrantedAuthority
>>
when
(
mockAuthentication
.
getAuthorities
()).
thenReturn
(
authorities
);
try
(
MockedStatic
<
CurrentUserHelper
>
mockUserHelper
=
Mockito
.
mockStatic
(
CurrentUserHelper
.
class
,
Mockito
.
CALLS_REAL_METHODS
)
){
mockUserHelper
.
when
(
CurrentUserHelper:
:
getAuthentication
).
thenReturn
(
mockAuthentication
);
mockUserHelper
.
when
(()
->
CurrentUserHelper
.
containsRole
(
Mockito
.
anyList
(),
Mockito
.
anyString
()))
.
thenReturn
(
false
);
boolean
hasRole
=
CurrentUserHelper
.
hasRole
(
UserRole
.
ADMIN_USER
);
assertThat
(
hasRole
).
isFalse
();
}
}
@Test
void
shouldHaveRole
()
{
Mockito
.
when
(
mockAuthentication
.
getPrincipal
()).
thenReturn
(
mockPrincipal
);
List
<
GrantedAuthority
>
authorities
=
List
.
of
();
Mockito
.<
Collection
<?
extends
GrantedAuthority
>>
when
(
mockAuthentication
.
getAuthorities
()).
thenReturn
(
authorities
);
try
(
MockedStatic
<
CurrentUserHelper
>
mockUserHelper
=
Mockito
.
mockStatic
(
CurrentUserHelper
.
class
,
Mockito
.
CALLS_REAL_METHODS
)
){
mockUserHelper
.
when
(
CurrentUserHelper:
:
getAuthentication
).
thenReturn
(
mockAuthentication
);
mockUserHelper
.
when
(()
->
CurrentUserHelper
.
containsRole
(
Mockito
.
anyList
(),
Mockito
.
anyString
()))
.
thenReturn
(
true
);
boolean
hasRole
=
CurrentUserHelper
.
hasRole
(
UserRole
.
ADMIN_USER
);
assertThat
(
hasRole
).
isTrue
();
}
}
}
@DisplayName
(
"Contains role"
)
@Nested
class
TestContainsRole
{
@Test
void
shouldNotContainRoleIfNull
()
{
boolean
containsRole
=
CurrentUserHelper
.
containsRole
(
null
,
UserRole
.
ADMIN_USER
);
assertThat
(
containsRole
).
isFalse
();
}
@Test
void
shouldNotContainRole
()
{
List
<
GrantedAuthority
>
authorities
=
List
.
of
(
new
SimpleGrantedAuthority
(
CurrentUserHelper
.
ROLE_PREFIX
+
"OTHER"
)
);
boolean
containsRole
=
CurrentUserHelper
.
containsRole
(
authorities
,
UserRole
.
ADMIN_USER
);
assertThat
(
containsRole
).
isFalse
();
}
@Test
void
shouldContainRole
()
{
Collection
<?
extends
GrantedAuthority
>
authorities
=
List
.
of
(
new
SimpleGrantedAuthority
(
CurrentUserHelper
.
ROLE_PREFIX
+
UserRole
.
ADMIN_USER
)
);
boolean
containsRole
=
CurrentUserHelper
.
containsRole
(
authorities
,
UserRole
.
ADMIN_USER
);
assertThat
(
containsRole
).
isTrue
();
}
}
public
class
CurrentUserHelperTest
{
@DisplayName
(
"Prepare role for check"
)
@Nested
class
TestPrepareRoleForCheck
{
...
...
@@ -66,69 +176,43 @@ public class CurrentUserHelperTest {
}
@Test
void
shouldReturnPassing
RoleIfNon
Null
()
{
void
shouldReturn
NullIf
PassingNull
()
{
var
role
=
CurrentUserHelper
.
prepareRoleForCheck
(
null
);
assertThat
(
role
).
isNull
();
}
}
@DisplayName
(
"
Has role
"
)
@DisplayName
(
"
Get authentication
"
)
@Nested
class
Test
ContainsRole
{
class
Test
GetAuthentication
{
@Mock
Authentication
mockAuthentication
=
Mockito
.
mock
(
Authentication
.
class
);
@Mock
SecurityContext
mockSecurityContext
=
Mockito
.
mock
(
SecurityContext
.
class
);
@BeforeEach
void
beforeEach
()
{
Mockito
.
when
(
mockSecurityContext
.
getAuthentication
()).
thenReturn
(
mockAuthentication
);
}
private
final
SecurityContext
mockSecurityContext
=
Mockito
.
mock
(
SecurityContext
.
class
);
@Test
void
shouldNotHaveRoleIfNull
()
{
Mockito
.
when
(
mockAuthentication
.
getAuthorities
()).
thenReturn
(
null
);
Mockito
.
when
(
mockAuthentication
.
getPrincipal
()).
thenReturn
(
null
);
void
shouldThrowIfNull
()
{
Mockito
.
when
(
mockSecurityContext
.
getAuthentication
()).
thenReturn
(
null
);
try
(
MockedStatic
<
SecurityContextHolder
>
contextHolder
=
Mockito
.
mockStatic
(
SecurityContextHolder
.
class
))
{
contextHolder
.
when
(
SecurityContextHolder:
:
getContext
).
thenReturn
(
mockSecurityContext
);
boolean
containsRole
=
CurrentUserHelper
.
hasRole
(
UserRole
.
ADMIN_USER
);
assertThat
(
containsRole
).
isFalse
(
);
assertThatIllegalStateException
()
.
isThrownBy
(
CurrentUserHelper:
:
getAuthentication
)
.
withMessage
(
"No authenticated User found"
);
}
}
@Test
void
shouldNotHaveRole
()
{
List
<
GrantedAuthority
>
authorities
=
List
.
of
(
new
SimpleGrantedAuthority
(
CurrentUserHelper
.
ROLE_PREFIX
+
"OTHER"
));
User
principal
=
new
User
(
"user"
,
"password"
,
authorities
);
Mockito
.<
Collection
<?
extends
GrantedAuthority
>>
when
(
mockAuthentication
.
getAuthorities
()).
thenReturn
(
authorities
);
Mockito
.
when
(
mockAuthentication
.
getPrincipal
()).
thenReturn
(
principal
);
try
(
MockedStatic
<
SecurityContextHolder
>
contextHolder
=
Mockito
.
mockStatic
(
SecurityContextHolder
.
class
))
{
contextHolder
.
when
(
SecurityContextHolder:
:
getContext
).
thenReturn
(
mockSecurityContext
);
boolean
containsRole
=
CurrentUserHelper
.
hasRole
(
UserRole
.
ADMIN_USER
);
assertThat
(
containsRole
).
isFalse
();
}
}
@Test
void
shouldHaveRole
()
{
Collection
<?
extends
GrantedAuthority
>
authorities
=
List
.
of
(
new
SimpleGrantedAuthority
(
CurrentUserHelper
.
ROLE_PREFIX
+
UserRole
.
ADMIN_USER
));
User
principal
=
new
User
(
"user"
,
"password"
,
authorities
);
Mockito
.<
Collection
<?
extends
GrantedAuthority
>>
when
(
mockAuthentication
.
getAuthorities
()).
thenReturn
(
authorities
);
Mockito
.
when
(
mockAuthentication
.
getPrincipal
()).
thenReturn
(
principal
);
void
shouldPassAuthentication
()
{
Authentication
mockAuthentication
=
Mockito
.
mock
(
Authentication
.
class
);
Mockito
.
when
(
mockSecurityContext
.
getAuthentication
()).
thenReturn
(
mockAuthentication
);
try
(
MockedStatic
<
SecurityContextHolder
>
contextHolder
=
Mockito
.
mockStatic
(
SecurityContextHolder
.
class
))
{
contextHolder
.
when
(
SecurityContextHolder:
:
getContext
).
thenReturn
(
mockSecurityContext
);
boolean
containsRole
=
CurrentUserHelper
.
hasRole
(
UserRole
.
ADMIN_USER
);
Authentication
authentication
=
CurrentUserHelper
.
getAuthentication
(
);
assertThat
(
containsRole
).
isTrue
(
);
assertThat
(
authentication
).
isSameAs
(
mockAuthentication
);
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment