-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for AddNewUserFilter.
- Loading branch information
Bill Smith
committed
Jan 22, 2019
1 parent
778fccf
commit 850690f
Showing
2 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
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
75 changes: 75 additions & 0 deletions
75
pac4j-module/src/test/groovy/net/unicon/shibui/pac4j/AddNewUserFilterTests.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,75 @@ | ||
package net.unicon.shibui.pac4j | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.security.model.Role | ||
import edu.internet2.tier.shibboleth.admin.ui.security.model.User | ||
import edu.internet2.tier.shibboleth.admin.ui.security.repository.RoleRepository | ||
import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository | ||
import edu.internet2.tier.shibboleth.admin.ui.service.EmailService | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.security.core.context.SecurityContext | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import spock.lang.Specification | ||
import spock.lang.Subject | ||
|
||
import javax.servlet.FilterChain | ||
import javax.servlet.ServletRequest | ||
import javax.servlet.http.HttpServletResponse | ||
|
||
/** | ||
* @author Bill Smith (wsmith@unicon.net) | ||
*/ | ||
class AddNewUserFilterTests extends Specification { | ||
|
||
UserRepository userRepository = Mock() | ||
RoleRepository roleRepository = Mock() | ||
EmailService emailService = Mock() | ||
|
||
ServletRequest request = Mock() | ||
HttpServletResponse response = Mock() | ||
FilterChain chain = Mock() | ||
|
||
SecurityContext securityContext = Mock() | ||
Authentication authentication = Mock() | ||
|
||
@Subject | ||
AddNewUserFilter addNewUserFilter = new AddNewUserFilter(userRepository, roleRepository, emailService) | ||
|
||
def setup() { | ||
SecurityContextHolder.setContext(securityContext) | ||
securityContext.getAuthentication() >> authentication | ||
} | ||
|
||
def "new users are redirected"() { | ||
given: | ||
authentication.getName() >> 'newUser' | ||
userRepository.findByUsername('newUser') >> Optional.empty() | ||
roleRepository.findByName('ROLE_NONE') >> Optional.of(new Role('ROLE_NONE')) | ||
|
||
when: | ||
addNewUserFilter.doFilter(request, response, chain) | ||
|
||
then: | ||
1 * roleRepository.save(_) | ||
1 * userRepository.save(_) | ||
1 * emailService.sendNewUserMail('newUser') | ||
1 * response.sendRedirect("/static.html") | ||
} | ||
|
||
def "existing users are not redirected"() { | ||
given: | ||
authentication.getName() >> 'existingUser' | ||
userRepository.findByUsername('existingUser') >> Optional.of(new User().with { | ||
it.username = 'existingUser' | ||
it.roles = [new Role('ROLE_USER')] | ||
it | ||
}) | ||
|
||
when: | ||
addNewUserFilter.doFilter(request, response, chain) | ||
|
||
then: | ||
0 * roleRepository.save(_) | ||
0 * userRepository.save(_) | ||
1 * chain.doFilter(_, _) | ||
} | ||
} |