|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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.security.web.jackson2; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.skyscreamer.jsonassert.JSONAssert; |
| 22 | + |
| 23 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 24 | +import org.springframework.security.core.Authentication; |
| 25 | +import org.springframework.security.core.authority.AuthorityUtils; |
| 26 | +import org.springframework.security.jackson2.AbstractMixinTests; |
| 27 | +import org.springframework.security.jackson2.SimpleGrantedAuthorityMixinTests; |
| 28 | +import org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Markus Heiden |
| 34 | + * @since 5.8 |
| 35 | + */ |
| 36 | +public class SwitchUserGrantedAuthorityMixInTest extends AbstractMixinTests { |
| 37 | + |
| 38 | + // language=JSON |
| 39 | + private static final String SWITCH_JSON = """ |
| 40 | + { |
| 41 | + "@class": "org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority", |
| 42 | + "role": "switched", |
| 43 | + "source": { |
| 44 | + "@class": "org.springframework.security.authentication.UsernamePasswordAuthenticationToken", |
| 45 | + "principal": "principal", |
| 46 | + "credentials": "credentials", |
| 47 | + "authenticated": true, |
| 48 | + "details": null, |
| 49 | + "authorities": %s |
| 50 | + } |
| 51 | + } |
| 52 | + """.formatted(SimpleGrantedAuthorityMixinTests.AUTHORITIES_ARRAYLIST_JSON); |
| 53 | + SwitchUserGrantedAuthority expected; |
| 54 | + |
| 55 | + Authentication source; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + public void setupExpected() { |
| 59 | + this.source = new UsernamePasswordAuthenticationToken("principal", "credentials", |
| 60 | + AuthorityUtils.createAuthorityList("ROLE_USER")); |
| 61 | + this.expected = new SwitchUserGrantedAuthority("switched", this.source); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void serializeWhenPrincipalCredentialsAuthoritiesThenSuccess() throws Exception { |
| 66 | + String serializedJson = this.mapper.writeValueAsString(this.expected); |
| 67 | + JSONAssert.assertEquals(SWITCH_JSON, serializedJson, true); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenMixinTest() throws Exception { |
| 72 | + SwitchUserGrantedAuthority deserialized = this.mapper.readValue(SWITCH_JSON, SwitchUserGrantedAuthority.class); |
| 73 | + assertThat(deserialized).isNotNull(); |
| 74 | + assertThat(deserialized.getAuthority()).isEqualTo("switched"); |
| 75 | + assertThat(deserialized.getSource()).isEqualTo(this.source); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments