| 1 |
|
package net.sourceforge.pebble.security; |
| 2 |
|
|
| 3 |
|
import junit.framework.TestCase; |
| 4 |
|
import net.sourceforge.pebble.Constants; |
| 5 |
|
import org.acegisecurity.userdetails.UserDetails; |
| 6 |
|
import org.acegisecurity.userdetails.UsernameNotFoundException; |
| 7 |
|
import org.acegisecurity.GrantedAuthorityImpl; |
| 8 |
|
|
| 9 |
|
import java.util.Arrays; |
| 10 |
|
import java.util.List; |
| 11 |
|
import java.util.HashMap; |
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
@author |
| 17 |
|
|
|
|
|
| 95,7% |
Uncovered Elements: 1 (23) |
Complexity: 5 |
Complexity Density: 0,26 |
|
| 18 |
|
public class DefaultUserDetailsServiceTest extends TestCase { |
| 19 |
|
|
| 20 |
|
private DefaultUserDetailsService service; |
| 21 |
|
private SecurityRealm securityRealm; |
| 22 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 23 |
6
|
protected void setUp() {... |
| 24 |
6
|
service = new DefaultUserDetailsService(); |
| 25 |
6
|
securityRealm = new MockSecurityRealm(); |
| 26 |
6
|
service.setSecurityRealm(securityRealm); |
| 27 |
|
} |
| 28 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
| 29 |
2
|
public void testSecurityRealm() {... |
| 30 |
2
|
assertSame(securityRealm, service.getSecurityRealm()); |
| 31 |
|
} |
| 32 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0,1 |
1
PASS
|
|
| 33 |
2
|
public void testLoadByUsername() throws Exception {... |
| 34 |
2
|
PebbleUserDetails pud = new PebbleUserDetails("username", "password", "name", "emailAddress", "website", "profile", new String[]{Constants.BLOG_OWNER_ROLE}, new HashMap<String,String>(), true); |
| 35 |
2
|
securityRealm.createUser(pud); |
| 36 |
2
|
UserDetails user = service.loadUserByUsername("username"); |
| 37 |
|
|
| 38 |
2
|
assertNotNull(user); |
| 39 |
2
|
assertEquals("username", user.getUsername()); |
| 40 |
2
|
assertEquals("password", user.getPassword()); |
| 41 |
|
|
| 42 |
2
|
List authorities = Arrays.asList(user.getAuthorities()); |
| 43 |
2
|
assertEquals(2, authorities.size()); |
| 44 |
2
|
assertTrue(authorities.contains(new GrantedAuthorityImpl(Constants.BLOG_OWNER_ROLE))); |
| 45 |
2
|
assertTrue(authorities.contains(new GrantedAuthorityImpl(Constants.BLOG_READER_ROLE))); |
| 46 |
|
} |
| 47 |
|
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0,4 |
1
PASS
|
|
| 48 |
2
|
public void testLoadByUsernameThrowsExceptionWhenUserDoesntExist() throws Exception {... |
| 49 |
2
|
try { |
| 50 |
2
|
PebbleUserDetails pud = new PebbleUserDetails("username", "password", "name", "emailAddress", "website", "profile", new String[]{Constants.BLOG_OWNER_ROLE}, new HashMap<String,String>(), true); |
| 51 |
2
|
securityRealm.createUser(pud); |
| 52 |
2
|
UserDetails user = service.loadUserByUsername("someotherusername"); |
| 53 |
0
|
fail(); |
| 54 |
|
} catch (UsernameNotFoundException e) { |
| 55 |
|
} |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
} |