Closed pmverma closed 6 years ago
It's normal that it doesn't work in tests since they use a mock and security is not applied.
What does it give with a real server ?
To apply security in test, add apply(springSecurity())
to restUserMockMvc
@cbornet with real server, it is fine. And thank you for the tip. However I am still having problem and I have posted on SO.
https://stackoverflow.com/questions/50921852
I will close this issue as this is SO question. Thanks
We should apply and verify security in the tests. This has been talked about many times but I guess nobody worked on it... As always PRs are very much welcome :smile:.
It's normal that it doesn't work in tests since they use a mock and security is not applied. What does it give with a real server ? To apply security in test, add apply(springSecurity()) to restUserMockMvc
@cbornet Can you give a little bit more detail? When I do what you are suggesting I get this exception:
springSecurityFilterChain cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
java.lang.IllegalStateException: springSecurityFilterChain cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
Edit: I got it working. See https://github.com/jhipster/generator-jhipster/issues/7374#issuecomment-417118011
Overview of the issue
Annotating
@Secured(AuthoritiesConstants.ADMIN)
is not workingMotivation for or Use Case
Annotating with
@Secured
must work as expected, in this case, other than ADMIN role must fail, but thats not happening.Reproduce the error
Generate a fresh application
Change following test method of class
UserResourceIntTest.java
Changeto
Run test,
./mvn clean test
All tests are being passed although user creation is allowed only to admin role.
Related issues
7374
Suggest a Fix
-
JHipster Version(s)
JHipster configuration, a
.yo-rc.json
file generated in the root folderJDL for the Entity configuration(s)
entityName.json
files generated in the.jhipster
directoryls: no such file or directory: .jhipster/*.json
JDL entity definitions
Environment and Tools
java version "1.8.0_172" Java(TM) SE Runtime Environment (build 1.8.0_172-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
git version 2.15.1 (Apple Git-101)
node: v8.11.2
npm: 6.0.1
yeoman: 2.0.2
yarn: 1.7.0
Docker version 18.03.1-ce, build 9ee9f40
docker-compose version 1.21.1, build 5a3f1a3