|
| 1 | +/* |
| 2 | + * Copyright 2014-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.session.hazelcast; |
| 18 | + |
| 19 | +import com.hazelcast.core.HazelcastInstance; |
| 20 | +import com.hazelcast.map.IMap; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 25 | +import org.springframework.security.core.Authentication; |
| 26 | +import org.springframework.security.core.authority.AuthorityUtils; |
| 27 | +import org.springframework.security.core.context.SecurityContext; |
| 28 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 29 | +import org.springframework.session.MapSession; |
| 30 | +import org.springframework.session.hazelcast.Hazelcast4IndexedSessionRepository.HazelcastSession; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * Base class for {@link Hazelcast4IndexedSessionRepository} integration tests. |
| 36 | + * |
| 37 | + * @author Eleftheria Stein |
| 38 | + */ |
| 39 | +abstract class AbstractHazelcast4IndexedSessionRepositoryITests { |
| 40 | + |
| 41 | + private static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT"; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private HazelcastInstance hazelcastInstance; |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private Hazelcast4IndexedSessionRepository repository; |
| 48 | + |
| 49 | + @Test |
| 50 | + void createAndDestroySession() { |
| 51 | + HazelcastSession sessionToSave = this.repository.createSession(); |
| 52 | + String sessionId = sessionToSave.getId(); |
| 53 | + |
| 54 | + IMap<String, MapSession> hazelcastMap = this.hazelcastInstance |
| 55 | + .getMap(Hazelcast4IndexedSessionRepository.DEFAULT_SESSION_MAP_NAME); |
| 56 | + |
| 57 | + assertThat(hazelcastMap.size()).isEqualTo(0); |
| 58 | + |
| 59 | + this.repository.save(sessionToSave); |
| 60 | + |
| 61 | + assertThat(hazelcastMap.size()).isEqualTo(1); |
| 62 | + assertThat(hazelcastMap.get(sessionId)).isEqualTo(sessionToSave); |
| 63 | + |
| 64 | + this.repository.deleteById(sessionId); |
| 65 | + |
| 66 | + assertThat(hazelcastMap.size()).isEqualTo(0); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void changeSessionIdWhenOnlyChangeId() { |
| 71 | + String attrName = "changeSessionId"; |
| 72 | + String attrValue = "changeSessionId-value"; |
| 73 | + HazelcastSession toSave = this.repository.createSession(); |
| 74 | + toSave.setAttribute(attrName, attrValue); |
| 75 | + |
| 76 | + this.repository.save(toSave); |
| 77 | + |
| 78 | + HazelcastSession findById = this.repository.findById(toSave.getId()); |
| 79 | + |
| 80 | + assertThat(findById.<String>getAttribute(attrName)).isEqualTo(attrValue); |
| 81 | + |
| 82 | + String originalFindById = findById.getId(); |
| 83 | + String changeSessionId = findById.changeSessionId(); |
| 84 | + |
| 85 | + this.repository.save(findById); |
| 86 | + |
| 87 | + assertThat(this.repository.findById(originalFindById)).isNull(); |
| 88 | + |
| 89 | + HazelcastSession findByChangeSessionId = this.repository.findById(changeSessionId); |
| 90 | + |
| 91 | + assertThat(findByChangeSessionId.<String>getAttribute(attrName)).isEqualTo(attrValue); |
| 92 | + |
| 93 | + this.repository.deleteById(changeSessionId); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void changeSessionIdWhenChangeTwice() { |
| 98 | + HazelcastSession toSave = this.repository.createSession(); |
| 99 | + |
| 100 | + this.repository.save(toSave); |
| 101 | + |
| 102 | + String originalId = toSave.getId(); |
| 103 | + String changeId1 = toSave.changeSessionId(); |
| 104 | + String changeId2 = toSave.changeSessionId(); |
| 105 | + |
| 106 | + this.repository.save(toSave); |
| 107 | + |
| 108 | + assertThat(this.repository.findById(originalId)).isNull(); |
| 109 | + assertThat(this.repository.findById(changeId1)).isNull(); |
| 110 | + assertThat(this.repository.findById(changeId2)).isNotNull(); |
| 111 | + |
| 112 | + this.repository.deleteById(changeId2); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void changeSessionIdWhenSetAttributeOnChangedSession() { |
| 117 | + String attrName = "changeSessionId"; |
| 118 | + String attrValue = "changeSessionId-value"; |
| 119 | + |
| 120 | + HazelcastSession toSave = this.repository.createSession(); |
| 121 | + |
| 122 | + this.repository.save(toSave); |
| 123 | + |
| 124 | + HazelcastSession findById = this.repository.findById(toSave.getId()); |
| 125 | + |
| 126 | + findById.setAttribute(attrName, attrValue); |
| 127 | + |
| 128 | + String originalFindById = findById.getId(); |
| 129 | + String changeSessionId = findById.changeSessionId(); |
| 130 | + |
| 131 | + this.repository.save(findById); |
| 132 | + |
| 133 | + assertThat(this.repository.findById(originalFindById)).isNull(); |
| 134 | + |
| 135 | + HazelcastSession findByChangeSessionId = this.repository.findById(changeSessionId); |
| 136 | + |
| 137 | + assertThat(findByChangeSessionId.<String>getAttribute(attrName)).isEqualTo(attrValue); |
| 138 | + |
| 139 | + this.repository.deleteById(changeSessionId); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void changeSessionIdWhenHasNotSaved() { |
| 144 | + HazelcastSession toSave = this.repository.createSession(); |
| 145 | + String originalId = toSave.getId(); |
| 146 | + toSave.changeSessionId(); |
| 147 | + |
| 148 | + this.repository.save(toSave); |
| 149 | + |
| 150 | + assertThat(this.repository.findById(toSave.getId())).isNotNull(); |
| 151 | + assertThat(this.repository.findById(originalId)).isNull(); |
| 152 | + |
| 153 | + this.repository.deleteById(toSave.getId()); |
| 154 | + } |
| 155 | + |
| 156 | + @Test // gh-1076 |
| 157 | + void attemptToUpdateSessionAfterDelete() { |
| 158 | + HazelcastSession session = this.repository.createSession(); |
| 159 | + String sessionId = session.getId(); |
| 160 | + this.repository.save(session); |
| 161 | + session = this.repository.findById(sessionId); |
| 162 | + session.setAttribute("attributeName", "attributeValue"); |
| 163 | + this.repository.deleteById(sessionId); |
| 164 | + this.repository.save(session); |
| 165 | + |
| 166 | + assertThat(this.repository.findById(sessionId)).isNull(); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + void createAndUpdateSession() { |
| 171 | + HazelcastSession session = this.repository.createSession(); |
| 172 | + String sessionId = session.getId(); |
| 173 | + |
| 174 | + this.repository.save(session); |
| 175 | + |
| 176 | + session = this.repository.findById(sessionId); |
| 177 | + session.setAttribute("attributeName", "attributeValue"); |
| 178 | + |
| 179 | + this.repository.save(session); |
| 180 | + |
| 181 | + assertThat(this.repository.findById(sessionId)).isNotNull(); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + void createSessionWithSecurityContextAndFindById() { |
| 186 | + HazelcastSession session = this.repository.createSession(); |
| 187 | + String sessionId = session.getId(); |
| 188 | + |
| 189 | + Authentication authentication = new UsernamePasswordAuthenticationToken("saves-" + System.currentTimeMillis(), |
| 190 | + "password", AuthorityUtils.createAuthorityList("ROLE_USER")); |
| 191 | + SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); |
| 192 | + securityContext.setAuthentication(authentication); |
| 193 | + session.setAttribute(SPRING_SECURITY_CONTEXT, securityContext); |
| 194 | + |
| 195 | + this.repository.save(session); |
| 196 | + |
| 197 | + assertThat(this.repository.findById(sessionId)).isNotNull(); |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments