|
| 1 | +package specs |
| 2 | + |
| 3 | +import org.springframework.security.core.userdetails.UserCache |
| 4 | +import pages.LoginPage |
| 5 | +import pages.role.CreateRolePage |
| 6 | +import pages.role.ListRolePage |
| 7 | +import pages.role.ShowRolePage |
| 8 | +import pages.user.CreateUserPage |
| 9 | +import pages.user.ListUserPage |
| 10 | +import pages.user.ShowUserPage |
| 11 | +import spock.lang.IgnoreIf |
| 12 | + |
| 13 | +@IgnoreIf({ System.getProperty('TESTCONFIG') != 'basicCacheUsers' }) |
| 14 | +class BasicAuthCacheUsersSecuritySpec extends AbstractSecuritySpec { |
| 15 | + |
| 16 | + private HttpURLConnection connection |
| 17 | + UserCache userCache |
| 18 | + |
| 19 | + void 'create roles'() { |
| 20 | + when: |
| 21 | + to ListRolePage |
| 22 | + |
| 23 | + then: |
| 24 | + roleRows.size() == 0 |
| 25 | + |
| 26 | + when: |
| 27 | + newRoleButton.click() |
| 28 | + |
| 29 | + then: |
| 30 | + at CreateRolePage |
| 31 | + |
| 32 | + when: |
| 33 | + authority = 'ROLE_ADMIN' |
| 34 | + createButton.click() |
| 35 | + |
| 36 | + then: |
| 37 | + at ShowRolePage |
| 38 | + |
| 39 | + when: |
| 40 | + to ListRolePage |
| 41 | + |
| 42 | + then: |
| 43 | + roleRows.size() == 1 |
| 44 | + |
| 45 | + when: |
| 46 | + newRoleButton.click() |
| 47 | + |
| 48 | + then: |
| 49 | + at CreateRolePage |
| 50 | + |
| 51 | + when: |
| 52 | + authority = 'ROLE_ADMIN2' |
| 53 | + createButton.click() |
| 54 | + |
| 55 | + then: |
| 56 | + at ShowRolePage |
| 57 | + |
| 58 | + when: |
| 59 | + to ListRolePage |
| 60 | + |
| 61 | + then: |
| 62 | + roleRows.size() == 2 |
| 63 | + } |
| 64 | + |
| 65 | + void 'create users'() { |
| 66 | + when: |
| 67 | + to ListUserPage |
| 68 | + |
| 69 | + then: |
| 70 | + userRows.size() == 0 |
| 71 | + |
| 72 | + when: |
| 73 | + newUserButton.click() |
| 74 | + |
| 75 | + then: |
| 76 | + at CreateUserPage |
| 77 | + |
| 78 | + when: |
| 79 | + username = 'admin1' |
| 80 | + password = 'password1' |
| 81 | + $('#enabled').click() |
| 82 | + $('#ROLE_ADMIN').click() |
| 83 | + createButton.click() |
| 84 | + |
| 85 | + then: |
| 86 | + at ShowUserPage |
| 87 | + |
| 88 | + when: |
| 89 | + to ListUserPage |
| 90 | + |
| 91 | + then: |
| 92 | + userRows.size() == 1 |
| 93 | + |
| 94 | + when: |
| 95 | + newUserButton.click() |
| 96 | + |
| 97 | + then: |
| 98 | + at CreateUserPage |
| 99 | + |
| 100 | + when: |
| 101 | + username = 'admin2' |
| 102 | + password = 'password2' |
| 103 | + $('#enabled').click() |
| 104 | + $('#ROLE_ADMIN').click() |
| 105 | + $('#ROLE_ADMIN2').click() |
| 106 | + createButton.click() |
| 107 | + |
| 108 | + then: |
| 109 | + at ShowUserPage |
| 110 | + |
| 111 | + when: |
| 112 | + to ListUserPage |
| 113 | + |
| 114 | + then: |
| 115 | + userRows.size() == 2 |
| 116 | + } |
| 117 | + |
| 118 | + @IgnoreIf({ !System.getProperty('geb.env') }) |
| 119 | + void 'check userDetails caching'() { |
| 120 | + |
| 121 | + when: |
| 122 | + go 'secureAnnotated' |
| 123 | + |
| 124 | + then: |
| 125 | + at LoginPage |
| 126 | + |
| 127 | + when: |
| 128 | + login 'admin1', 'password1' |
| 129 | + |
| 130 | + then: |
| 131 | + assertContentContains 'you have ROLE_ADMIN' |
| 132 | + |
| 133 | + and: |
| 134 | + userCache.getUserFromCache('admin1') |
| 135 | + |
| 136 | + cleanup: |
| 137 | + logout() |
| 138 | + } |
| 139 | + |
| 140 | + protected void logout() { |
| 141 | + super.logout() |
| 142 | + // cheesy, but the 'Authentication' header from basic auth |
| 143 | + // isn't cleared, so this forces an invalid header |
| 144 | + getWithAuth '', 'not_a_valid_username', '' |
| 145 | + } |
| 146 | + |
| 147 | + private void getWithAuth(String path, String username, String password) { |
| 148 | + String uri = new URI(baseUrlRequired).resolve(new URI(path)) |
| 149 | + go uri.replace('http://', 'http://' + username + ':' + password + '@') |
| 150 | + } |
| 151 | +} |
0 commit comments