3 * * ============LICENSE_START=======================================================
5 * * ================================================================================
6 * * Copyright (c) 2020 - 2021 AT&T Intellectual Property. All rights reserved.
7 * * ================================================================================
8 * * Licensed under the Apache License, Version 2.0 (the "License");
9 * * you may not use this file except in compliance with the License.
10 * * You may obtain a copy of the License at
12 * * http://www.apache.org/licenses/LICENSE-2.0
14 * * Unless required by applicable law or agreed to in writing, software
15 * * distributed under the License is distributed on an "AS IS" BASIS,
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * * See the License for the specific language governing permissions and
18 * * limitations under the License.
19 * * ============LICENSE_END=========================================================
23 package org.onap.dcaegen2.platform.mod.web;
25 import static org.hamcrest.core.IsNull.notNullValue;
26 import static org.mockito.Mockito.any;
27 import static org.mockito.Mockito.anyString;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.asJsonString;
32 import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getLoginRequest;
33 import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getModUser;
34 import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getSignupRequest;
35 import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getUserDetailsImpl;
36 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
37 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
38 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
40 import java.util.Optional;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.mockito.Mock;
44 import org.onap.dcaegen2.platform.mod.controllers.AuthController;
45 import org.onap.dcaegen2.platform.mod.models.LoginRequest;
46 import org.onap.dcaegen2.platform.mod.models.Role;
47 import org.onap.dcaegen2.platform.mod.models.SignupRequest;
48 import org.onap.dcaegen2.platform.mod.repositories.RoleRepository;
49 import org.onap.dcaegen2.platform.mod.repositories.UserRepository;
50 import org.onap.dcaegen2.platform.mod.security.jwt.AuthEntryPointJwt;
51 import org.onap.dcaegen2.platform.mod.security.jwt.JwtUtils;
52 import org.onap.dcaegen2.platform.mod.security.services.UserDetailsServiceImpl;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
55 import org.springframework.boot.test.mock.mockito.MockBean;
56 import org.springframework.http.MediaType;
57 import org.springframework.security.authentication.AuthenticationManager;
58 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
59 import org.springframework.security.core.Authentication;
60 import org.springframework.security.crypto.password.PasswordEncoder;
61 import org.springframework.security.test.context.support.WithMockUser;
62 import org.springframework.test.web.servlet.MockMvc;
67 * Mock Test cases for AuthenticationController
69 @WebMvcTest(AuthController.class)
70 public class AuthControllerTest {
73 private MockMvc mockMvc;
79 AuthenticationManager authenticationManager;
82 UserRepository userRepository;
85 RoleRepository roleRepository;
88 PasswordEncoder passwordEncoder;
91 UserDetailsServiceImpl userDetailsService;
94 AuthEntryPointJwt authEntryPointJwt;
97 Authentication authentication;
105 void test_signin_returnsSuccessResponse() throws Exception {
107 LoginRequest loginRequest = getLoginRequest();
109 when(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()))).thenReturn(authentication);
110 when(authentication.getPrincipal()).thenReturn(getUserDetailsImpl());
111 when(jwtUtils.generateJwtToken(any())).thenReturn("Demo");
113 mockMvc.perform(post("/api/auth/signin")
114 .contentType(MediaType.APPLICATION_JSON)
115 .content(asJsonString(loginRequest)).accept(MediaType.APPLICATION_JSON))
116 .andExpect(jsonPath("$.token", notNullValue()))
117 .andExpect(status().isOk());
119 verify(authenticationManager, times(1)).authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
120 verify(jwtUtils, times(1)).generateJwtToken(any());
124 @WithMockUser(roles="ADMIN")
126 void test_signup_returnsSuccessResponse() throws Exception {
128 SignupRequest signUpRequest = getSignupRequest();
130 when(userRepository.existsByUsername(signUpRequest.getUsername())).thenReturn(false);
131 when(passwordEncoder.encode(signUpRequest.getPassword())).thenReturn("password");
132 when(roleRepository.findByName(anyString())).thenReturn(Optional.of(new Role()));
133 when(userRepository.save(any())).thenReturn(getModUser());
135 mockMvc.perform(post("/api/auth/signup")
136 .contentType(MediaType.APPLICATION_JSON)
137 .content(asJsonString(signUpRequest)).accept(MediaType.APPLICATION_JSON))
138 .andExpect(jsonPath("$.message", notNullValue()))
139 .andExpect(status().isOk());
141 verify(userRepository, times(1)).existsByUsername(signUpRequest.getUsername());
142 verify(passwordEncoder, times(1)).encode(signUpRequest.getPassword());
143 verify(roleRepository, times(1)).findByName(anyString());
144 verify(userRepository, times(1)).save(any());