Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LabraCORE
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
EIP
Labrador
LabraCORE
Commits
a3db4ba5
You need to sign in or sign up before continuing.
Commit
a3db4ba5
authored
May 14, 2020
by
Chris Lemaire
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for RoleController
parent
b3fe1e2d
No related branches found
No related tags found
2 merge requests
!75
Development
,
!45
Resolve "RoleController testing"
Pipeline
#324622
passed
May 14, 2020
Stage: build 1
Stage: test
Stage: review
Stage: publish
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/test/java/nl/tudelft/labracore/controller/RoleControllerTest.java
+137
-0
137 additions, 0 deletions
...a/nl/tudelft/labracore/controller/RoleControllerTest.java
with
137 additions
and
0 deletions
src/test/java/nl/tudelft/labracore/controller/RoleControllerTest.java
0 → 100644
+
137
−
0
View file @
a3db4ba5
/*
* Labracore - A connecting core service for Labrador products
* Copyright (C) 2020- Delft University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package
nl.tudelft.labracore.controller
;
import
static
nl
.
tudelft
.
labracore
.
model
.
RoleType
.*;
import
static
nl
.
tudelft
.
labracore
.
test
.
JsonContentMatcher
.
jsonContent
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
springframework
.
security
.
test
.
web
.
servlet
.
request
.
SecurityMockMvcRequestPostProcessors
.
csrf
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultHandlers
.
print
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
import
java.util.List
;
import
javax.transaction.Transactional
;
import
nl.tudelft.labracore.dto.create.RoleCreateDTO
;
import
nl.tudelft.labracore.dto.id.EditionIdDTO
;
import
nl.tudelft.labracore.dto.id.PersonIdDTO
;
import
nl.tudelft.labracore.dto.patch.RolePatchDTO
;
import
nl.tudelft.labracore.model.Edition
;
import
nl.tudelft.labracore.model.Role
;
import
nl.tudelft.labracore.repository.EditionRepository
;
import
nl.tudelft.labracore.repository.RoleRepository
;
import
nl.tudelft.labracore.test.RestControllerTest
;
import
nl.tudelft.labracore.test.TestDatabaseLoader
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.params.ParameterizedTest
;
import
org.junit.jupiter.params.provider.MethodSource
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.security.test.context.support.WithUserDetails
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
@Transactional
@SpringBootTest
@AutoConfigureMockMvc
public
class
RoleControllerTest
extends
RestControllerTest
{
@Autowired
private
MockMvc
mvc
;
@Autowired
private
TestDatabaseLoader
db
;
@Autowired
private
RoleRepository
rr
;
@Autowired
private
EditionRepository
er
;
private
Role
r
;
private
Edition
e
;
@BeforeEach
void
setup
()
{
r
=
rr
.
getOne
(
db
.
getPantherRoleCW
().
getId
());
e
=
er
.
getOne
(
db
.
getNewYork
().
getId
());
}
@Test
@WithUserDetails
(
"All-access Key"
)
void
addRoleValidatesDTO
()
throws
Exception
{
mvc
.
perform
(
postJson
(
"/api/role"
,
new
RoleCreateDTO
()))
.
andExpect
(
status
().
isUnprocessableEntity
());
}
@Test
@WithUserDetails
(
"All-access Key"
)
void
addRoleReturnsIdOfSavedRole
()
throws
Exception
{
mvc
.
perform
(
postJson
(
"/api/role"
,
RoleCreateDTO
.
builder
()
.
person
(
new
PersonIdDTO
(
r
.
getPerson
().
getId
()))
.
edition
(
new
EditionIdDTO
(
e
.
getId
()))
.
type
(
TEACHER_RO
)
.
build
()))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonContent
(
Role
.
Id
.
class
)
.
predicate
(
id
->
id
!=
null
&&
id
.
getEditionId
()
>
0
&&
id
.
getPersonId
()
>
0
&&
rr
.
findById
(
id
).
isPresent
()))
.
andDo
(
print
());
}
@Test
@WithUserDetails
(
"All-access Key"
)
void
patchRolePerformsUpdate
()
throws
Exception
{
mvc
.
perform
(
patchJson
(
"/api/role/"
+
r
.
getId
().
getPersonId
()
+
"/"
+
r
.
getId
().
getEditionId
(),
RolePatchDTO
.
builder
()
.
type
(
TEACHER
).
build
()));
assertThat
(
rr
.
findByIdOrThrow
(
r
.
getId
()).
getType
())
.
isEqualTo
(
TEACHER
);
}
@Test
@WithUserDetails
(
"All-access Key"
)
void
patchRoleReturnsSameId
()
throws
Exception
{
mvc
.
perform
(
patchJson
(
"/api/role/"
+
r
.
getId
().
getPersonId
()
+
"/"
+
r
.
getId
().
getEditionId
(),
RolePatchDTO
.
builder
()
.
type
(
STUDENT
).
build
()))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonContent
(
Role
.
Id
.
class
).
isEqualTo
(
r
.
getId
()));
}
@WithUserDetails
(
"No-access Key"
)
@MethodSource
(
"protectedEndpoints"
)
@ParameterizedTest
void
endpointsShouldBeKeyProtected
(
MockHttpServletRequestBuilder
request
)
throws
Exception
{
mvc
.
perform
(
request
.
with
(
csrf
()))
.
andExpect
(
status
().
isForbidden
());
}
private
static
List
<
MockHttpServletRequestBuilder
>
protectedEndpoints
()
throws
JsonProcessingException
{
return
List
.
of
(
postJson
(
"/api/role"
,
new
RoleCreateDTO
()),
patchJson
(
"/api/role/1/1"
,
new
RolePatchDTO
()));
}
}
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
sign in
to comment