|
| 1 | +/* |
| 2 | + * Copyright 2002-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 | +package org.springframework.security.ldap.jackson2; |
| 17 | + |
| 18 | +import org.springframework.ldap.core.DirContextAdapter; |
| 19 | +import org.springframework.ldap.core.DistinguishedName; |
| 20 | +import org.springframework.security.jackson2.SecurityJackson2Modules; |
| 21 | +import org.springframework.security.ldap.userdetails.InetOrgPerson; |
| 22 | +import org.springframework.security.ldap.userdetails.Person; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Disabled; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.skyscreamer.jsonassert.JSONAssert; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.junit.jupiter.api.Assertions.*; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link InetOrgPersonMixin}. |
| 35 | + */ |
| 36 | +class InetOrgPersonMixinTests { |
| 37 | + |
| 38 | + private ObjectMapper mapper; |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + public void setup() { |
| 42 | + ClassLoader loader = getClass().getClassLoader(); |
| 43 | + this.mapper = new ObjectMapper(); |
| 44 | + this.mapper.registerModules(SecurityJackson2Modules.getModules(loader)); |
| 45 | + } |
| 46 | + |
| 47 | + @Disabled |
| 48 | + @Test |
| 49 | + public void serializeWhenMixinRegisteredThenSerializes() throws Exception { |
| 50 | + InetOrgPerson.Essence essence = new InetOrgPerson.Essence(createUserContext()); |
| 51 | + InetOrgPerson p = (InetOrgPerson) essence.createUserDetails(); |
| 52 | + |
| 53 | + String expectedJson = asJson(p); |
| 54 | + String json = this.mapper.writeValueAsString(p); |
| 55 | + JSONAssert.assertEquals(expectedJson, json, true); |
| 56 | + } |
| 57 | + |
| 58 | + private DirContextAdapter createUserContext() { |
| 59 | + DirContextAdapter ctx = new DirContextAdapter(); |
| 60 | + ctx.setDn(new DistinguishedName("ignored=ignored")); |
| 61 | + ctx.setAttributeValue("uid", "ghengis"); |
| 62 | + ctx.setAttributeValue("userPassword", "pillage"); |
| 63 | + ctx.setAttributeValue("carLicense", "HORS1"); |
| 64 | + ctx.setAttributeValue("cn", "Ghengis Khan"); |
| 65 | + ctx.setAttributeValue("description", "Scary"); |
| 66 | + ctx.setAttributeValue("destinationIndicator", "West"); |
| 67 | + ctx.setAttributeValue("displayName", "Ghengis McCann"); |
| 68 | + ctx.setAttributeValue("givenName", "Ghengis"); |
| 69 | + ctx.setAttributeValue("homePhone", "+467575436521"); |
| 70 | + ctx.setAttributeValue("initials", "G"); |
| 71 | + ctx.setAttributeValue("employeeNumber", "00001"); |
| 72 | + ctx.setAttributeValue("homePostalAddress", "Steppes"); |
| 73 | + ctx.setAttributeValue("mail", "ghengis@mongolia"); |
| 74 | + ctx.setAttributeValue("mobile", "always"); |
| 75 | + ctx.setAttributeValue("o", "Hordes"); |
| 76 | + ctx.setAttributeValue("ou", "Horde1"); |
| 77 | + ctx.setAttributeValue("postalAddress", "On the Move"); |
| 78 | + ctx.setAttributeValue("postalCode", "Changes Frequently"); |
| 79 | + ctx.setAttributeValue("roomNumber", "Yurt 1"); |
| 80 | + ctx.setAttributeValue("roomNumber", "Yurt 1"); |
| 81 | + ctx.setAttributeValue("sn", "Khan"); |
| 82 | + ctx.setAttributeValue("street", "Westward Avenue"); |
| 83 | + ctx.setAttributeValue("telephoneNumber", "+442075436521"); |
| 84 | + return ctx; |
| 85 | + } |
| 86 | + |
| 87 | + private String asJson(Person person) { |
| 88 | + // @formatter:off |
| 89 | + return "{\n" + |
| 90 | + " \"@class\": \"org.springframework.security.ldap.userdetails.InetOrgPerson\"\n" + |
| 91 | + "}"; |
| 92 | + // @formatter:on |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments