|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.core.userdetails; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import org.springframework.security.core.GrantedAuthority; |
| 23 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 27 | + |
| 28 | +/** |
| 29 | + * Tests for {@link UserDetailsServiceAuthoritiesRepository} |
| 30 | + * |
| 31 | + * @author Marcus da Coregio |
| 32 | + */ |
| 33 | +class UserDetailsServiceAuthoritiesRepositoryTests { |
| 34 | + |
| 35 | + UserDetailsService userDetailsService = new InMemoryUserDetailsManager(PasswordEncodedUser.user(), |
| 36 | + PasswordEncodedUser.admin()); |
| 37 | + |
| 38 | + UserDetailsServiceAuthoritiesRepository userAuthoritiesRepository; |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + void setup() { |
| 42 | + this.userAuthoritiesRepository = new UserDetailsServiceAuthoritiesRepository(this.userDetailsService); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void findUserAuthoritiesWhenUserExistsThenReturn() { |
| 47 | + UserAuthorities admin = this.userAuthoritiesRepository.findAuthoritiesByUsername("admin"); |
| 48 | + assertThat(admin.getAuthorities()).extracting(GrantedAuthority::getAuthority) |
| 49 | + .containsExactly("ROLE_ADMIN", "ROLE_USER"); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void findUserAuthoritiesWhenUserDoesNotExistsThenUsernameNotFoundException() { |
| 54 | + assertThatExceptionOfType(UsernameNotFoundException.class) |
| 55 | + .isThrownBy(() -> this.userAuthoritiesRepository.findAuthoritiesByUsername("unknown")); |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments