3 * * ============LICENSE_START=======================================================
5 * * ================================================================================
6 * * Copyright (c) 2020 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 com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.gson.Gson;
27 import org.junit.Assert;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.mockito.Mock;
32 import org.onap.dcaegen2.platform.mod.controllers.AuthController;
33 import org.onap.dcaegen2.platform.mod.models.*;
34 import org.onap.dcaegen2.platform.mod.repositories.RoleRepository;
35 import org.onap.dcaegen2.platform.mod.repositories.UserRepository;
36 import org.onap.dcaegen2.platform.mod.security.jwt.AuthEntryPointJwt;
37 import org.onap.dcaegen2.platform.mod.security.jwt.JwtUtils;
38 import org.onap.dcaegen2.platform.mod.security.services.UserDetailsServiceImpl;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.boot.test.mock.mockito.MockBean;
43 import org.springframework.http.*;
44 import org.springframework.security.authentication.AuthenticationManager;
45 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
46 import org.springframework.security.core.Authentication;
47 import org.springframework.security.crypto.password.PasswordEncoder;
48 import org.springframework.security.test.context.support.WithMockUser;
49 import org.springframework.test.context.junit.jupiter.SpringExtension;
50 import org.springframework.test.web.servlet.MockMvc;
51 import org.springframework.web.client.RestTemplate;
52 import org.testng.annotations.BeforeTest;
54 import java.io.IOException;
55 import java.util.Optional;
57 import static org.hamcrest.core.IsNull.notNullValue;
58 import static org.mockito.Mockito.*;
59 import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.*;
60 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
61 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
62 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
63 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
68 * Mock Test cases for AuthenticationController
70 @WebMvcTest(AuthController.class)
71 public class AuthControllerTest {
74 private MockMvc mockMvc;
80 AuthenticationManager authenticationManager;
83 UserRepository userRepository;
86 RoleRepository roleRepository;
89 PasswordEncoder passwordEncoder;
92 UserDetailsServiceImpl userDetailsService;
95 AuthEntryPointJwt authEntryPointJwt;
98 Authentication authentication;
106 void test_signin_returnsSuccessResponse() throws Exception {
108 LoginRequest loginRequest = getLoginRequest();
110 when(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()))).thenReturn(authentication);
111 when(authentication.getPrincipal()).thenReturn(getUserDetailsImpl());
112 when(jwtUtils.generateJwtToken(any())).thenReturn("Demo");
114 mockMvc.perform(post("/api/auth/signin")
115 .contentType(MediaType.APPLICATION_JSON)
116 .content(asJsonString(loginRequest)).accept(MediaType.APPLICATION_JSON))
117 .andExpect(jsonPath("$.token", notNullValue()))
118 .andExpect(status().isOk());
120 verify(authenticationManager, times(1)).authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
121 verify(jwtUtils, times(1)).generateJwtToken(any());
125 @WithMockUser(roles="ADMIN")
127 void test_signup_returnsSuccessResponse() throws Exception {
129 SignupRequest signUpRequest = getSignupRequest();
131 when(userRepository.existsByUsername(signUpRequest.getUsername())).thenReturn(false);
132 when(passwordEncoder.encode(signUpRequest.getPassword())).thenReturn("password");
133 when(roleRepository.findByName(anyString())).thenReturn(Optional.of(new Role()));
134 when(userRepository.save(any())).thenReturn(getModUser());
136 mockMvc.perform(post("/api/auth/signup")
137 .contentType(MediaType.APPLICATION_JSON)
138 .content(asJsonString(signUpRequest)).accept(MediaType.APPLICATION_JSON))
139 .andExpect(jsonPath("$.message", notNullValue()))
140 .andExpect(status().isOk());
142 verify(userRepository, times(1)).existsByUsername(signUpRequest.getUsername());
143 verify(passwordEncoder, times(1)).encode(signUpRequest.getPassword());
144 verify(roleRepository, times(1)).findByName(anyString());
145 verify(userRepository, times(1)).save(any());