Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
backend
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
idatt2106-v23-03
backend
Commits
d4c8f522
Commit
d4c8f522
authored
2 years ago
by
Anders Austlid
Browse files
Options
Downloads
Patches
Plain Diff
Added TokenService unit tests
parent
8efb9c3c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/test/java/ntnu/idatt2016/v233/SmartMat/service/TokenServiceTest.java
+61
-0
61 additions, 0 deletions
...tnu/idatt2016/v233/SmartMat/service/TokenServiceTest.java
with
61 additions
and
0 deletions
src/test/java/ntnu/idatt2016/v233/SmartMat/service/TokenServiceTest.java
0 → 100644
+
61
−
0
View file @
d4c8f522
package
ntnu.idatt2016.v233.SmartMat.service
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.authority.SimpleGrantedAuthority
;
import
org.springframework.security.oauth2.jwt.Jwt
;
import
org.springframework.security.oauth2.jwt.JwtEncoder
;
import
org.springframework.security.oauth2.jwt.JwtEncoderParameters
;
import
java.time.Instant
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertNotNull
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
public
class
TokenServiceTest
{
private
TokenService
tokenService
;
private
JwtEncoder
jwtEncoder
;
@BeforeEach
void
setUp
()
{
jwtEncoder
=
mock
(
JwtEncoder
.
class
);
tokenService
=
new
TokenService
(
jwtEncoder
);
}
@Test
void
testGenerateToken
()
{
// Given
Collection
<
SimpleGrantedAuthority
>
authorities
=
Arrays
.
asList
(
new
SimpleGrantedAuthority
(
"ROLE_ADMIN"
),
new
SimpleGrantedAuthority
(
"ROLE_USER"
)
);
Authentication
authentication
=
new
UsernamePasswordAuthenticationToken
(
"user"
,
"password"
,
authorities
);
Instant
now
=
Instant
.
now
();
Jwt
jwt
=
Jwt
.
withTokenValue
(
"test-token"
)
.
header
(
"alg"
,
"none"
)
.
claim
(
"iss"
,
"self"
)
.
claim
(
"issuedAt"
,
now
)
.
claim
(
"expiresAt"
,
now
)
.
claim
(
"sub"
,
"user"
)
.
claim
(
"scope"
,
"ROLE_ADMIN ROLE_USER"
)
.
build
();
when
(
jwtEncoder
.
encode
(
any
(
JwtEncoderParameters
.
class
))).
thenReturn
(
jwt
);
// When
String
token
=
tokenService
.
generateToken
(
authentication
);
// Then
assertNotNull
(
token
);
assertEquals
(
"test-token"
,
token
);
}
}
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