-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #213 from skni-kod/issue-185_2
#185 kodemy-search and commons tests
- Loading branch information
Showing
14 changed files
with
964 additions
and
7 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
commons/src/test/groovy/pl/sknikod/kodemycommons/data/AuditableSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package pl.sknikod.kodemycommons.data | ||
|
||
import org.springframework.security.core.Authentication | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import pl.sknikod.kodemycommons.security.UserPrincipal | ||
import spock.lang.Specification | ||
|
||
import java.time.LocalDateTime | ||
|
||
|
||
class AuditableSpec extends Specification { | ||
|
||
|
||
def "should update createdDate and createdBy on prePersist"() { | ||
given: "fake authentication" | ||
withAuthentication() | ||
|
||
and: "an instance of Auditable and initial state" | ||
def auditable = new Auditable() {} | ||
def beforeDate = auditable.getCreatedDate() | ||
def beforeCreatedBy = auditable.getCreatedBy() | ||
def dateBeforePrePersist = LocalDateTime.now() | ||
|
||
when: "onPrePersist is called" | ||
auditable.onPrePersist() | ||
|
||
then: "before call createdDate and createdBy are null" | ||
beforeDate == null | ||
beforeCreatedBy == null | ||
|
||
and: "after call createdDate and createdBy are set" | ||
auditable.getCreatedDate() != beforeDate | ||
auditable.getCreatedDate().isAfter(dateBeforePrePersist) || auditable.getCreatedDate().isEqual(dateBeforePrePersist) | ||
auditable.getCreatedBy() != null | ||
|
||
cleanup: | ||
clearAuthentication() | ||
} | ||
|
||
def "should update modifiedDate and modifiedBy on preUpdate"() { | ||
given: "fake authentication" | ||
withAuthentication() | ||
|
||
and: "an instance of Auditable and initial state" | ||
def auditable = new Auditable() {} | ||
def beforeDate = auditable.getModifiedDate() | ||
def beforeModifiedBy = auditable.getModifiedBy() | ||
def dateBeforePreUpdate = LocalDateTime.now() | ||
|
||
when: "onPreUpdate is called" | ||
auditable.onPreUpdate() | ||
|
||
then: "before call modifiedDate and modifiedBy are null" | ||
beforeDate == null | ||
beforeModifiedBy == null | ||
|
||
and: "after call modifiedDate and modifiedBy are set" | ||
auditable.getModifiedDate() != beforeDate | ||
auditable.getModifiedDate().isAfter(dateBeforePreUpdate) || auditable.getModifiedDate().isEqual(dateBeforePreUpdate) | ||
auditable.getModifiedBy() != null | ||
|
||
cleanup: | ||
clearAuthentication() | ||
} | ||
|
||
|
||
private void withAuthentication() { | ||
def userPrincipal = new UserPrincipal(1L, "username", Collections.emptyList()) | ||
def authentication = Mock(Authentication) { | ||
isAuthenticated() >> true | ||
getPrincipal() >> userPrincipal | ||
} | ||
SecurityContextHolder.getContext().setAuthentication(authentication) | ||
} | ||
|
||
private static void clearAuthentication() { | ||
SecurityContextHolder.clearContext() | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
commons/src/test/groovy/pl/sknikod/kodemycommons/security/AuthFacadeSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package pl.sknikod.kodemycommons.security | ||
|
||
import org.springframework.security.core.Authentication | ||
import org.springframework.security.core.GrantedAuthority | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import spock.lang.Specification | ||
|
||
class AuthFacadeSpec extends Specification { | ||
|
||
def setup() { | ||
clearAuthentication() | ||
} | ||
|
||
def cleanup() { | ||
clearAuthentication() | ||
} | ||
|
||
def "should get authentication"() { | ||
given: | ||
withAuthentication() | ||
when: | ||
def authentication = AuthFacade.getAuthentication() | ||
then: | ||
authentication.isPresent() | ||
} | ||
|
||
def "should check is authenticated"() { | ||
given: | ||
auth.call() | ||
when: | ||
def isAuthenticated = AuthFacade.isAuthenticated() | ||
then: | ||
isAuthenticated == expected | ||
|
||
where: | ||
auth || expected ; | ||
{withAuthentication(authenticated: true)} || true ; | ||
{withAuthentication(authenticated: false)} || false ; | ||
} | ||
|
||
def "should get current username"() { | ||
given: | ||
withAuthentication(username: "abc") | ||
when: | ||
def username = AuthFacade.getCurrentUsername() | ||
then: | ||
username == "abc" | ||
} | ||
|
||
def "should get current user principal"() { | ||
given: | ||
withAuthentication() | ||
when: | ||
def userPrincipal = AuthFacade.getCurrentUserPrincipal() | ||
then: | ||
userPrincipal.isPresent() | ||
} | ||
|
||
def "should find any authority"() { | ||
given: | ||
withAuthentication(authorities: List.of( | ||
new SimpleGrantedAuthority("ROLE_USER"), | ||
new SimpleGrantedAuthority("ROLE_ADMIN"))) | ||
when: | ||
def anyAuthority = AuthFacade.hasAnyAuthority("ROLE_SUPERADMIN", "ROLE_ADMIN") | ||
then: | ||
anyAuthority | ||
} | ||
|
||
def "should find authority"() { | ||
given: | ||
withAuthentication(authorities: List.of( | ||
new SimpleGrantedAuthority("ROLE_USER"), | ||
new SimpleGrantedAuthority("ROLE_ADMIN"))) | ||
when: | ||
def authority = AuthFacade.hasAuthority(role) | ||
then: | ||
authority == expected | ||
|
||
where: | ||
role || expected | ||
"ROLE_ADMIN" || true | ||
"ROLE_SUPERADMIN" || false | ||
} | ||
|
||
|
||
def withAuthentication(Map args = [:]) { | ||
Long id = args.containsKey('id') ? args.id : 1L | ||
String username = args.containsKey('username') ? args.username : "username" | ||
Collection<? extends GrantedAuthority> authorities = args.containsKey('authorities') ? args.authorities : Collections.emptyList() | ||
boolean authenticated = args.containsKey('authenticated') ? args.authenticated : true | ||
|
||
def userPrincipal = new UserPrincipal(id, username, authorities as Collection<SimpleGrantedAuthority>) | ||
def authentication = Mock(Authentication) { | ||
isAuthenticated() >> authenticated | ||
getPrincipal() >> userPrincipal | ||
getName() >> username | ||
getAuthorities() >> authorities | ||
} | ||
SecurityContextHolder.getContext().setAuthentication(authentication) | ||
} | ||
|
||
private static void clearAuthentication() { | ||
SecurityContextHolder.clearContext() | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
commons/src/test/groovy/pl/sknikod/kodemycommons/security/JwtProviderSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pl.sknikod.kodemycommons.security | ||
|
||
import org.springframework.security.core.authority.SimpleGrantedAuthority | ||
import spock.lang.Specification | ||
|
||
class JwtProviderSpec extends Specification { | ||
|
||
def "should generate delegation token"() { | ||
given: | ||
def jwtProvider = new JwtProvider(new FakeProperties()) | ||
String subject = "user123" | ||
String authority = "ROLE_ADMIN" | ||
when: | ||
def token = jwtProvider.generateDelegationToken(subject, authority) | ||
then: | ||
token != null | ||
token.id() != null | ||
token.value() != null | ||
token.expiration() > new Date() | ||
and: | ||
def parsed = jwtProvider.parseToken(token.value()) | ||
parsed.isSuccess() | ||
def result = parsed.get() | ||
result.bearerId == token.id() | ||
result.username == "user123" | ||
result.authorities.contains(new SimpleGrantedAuthority("ROLE_ADMIN")) | ||
} | ||
|
||
def "should generate user token"() { | ||
given: | ||
def jwtProvider = new JwtProvider(new FakeProperties()) | ||
def input = new JwtProvider.Input(5L, "user123", false, false, | ||
false, true, [new SimpleGrantedAuthority("ROLE_USER")] as Set) | ||
when: | ||
def token = jwtProvider.generateUserToken(input) | ||
then: | ||
token != null | ||
token.id() != null | ||
token.value() != null | ||
token.expiration() > new Date() | ||
and: | ||
def parsed = jwtProvider.parseToken(token.value()) | ||
parsed.isSuccess() | ||
def result = parsed.get() | ||
result.id == 5 | ||
result.bearerId == token.id() | ||
result.username == "user123" | ||
result.authorities.contains(new SimpleGrantedAuthority("ROLE_USER")) | ||
} | ||
|
||
|
||
|
||
static class FakeProperties extends JwtProvider.Properties { | ||
FakeProperties() { | ||
secretKey = 'YWJjZGVmZ2hjvbwjrW5vcHFyc3R1dnd4eXoxMjM0NTY3OnDMTIzNDU2Nzg5MDEyMzQ1Njc4OTAf=' | ||
bearerExpirationMin = 15 | ||
delegationExpirationMin = 60 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.