Auth Service Errors Fix
[dcaegen2/platform.git] / mod2 / auth-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / AuthControllerTest.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
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
11  *  *
12  *  *       http://www.apache.org/licenses/LICENSE-2.0
13  *  *
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=========================================================
20  *
21  */
22
23 package org.onap.dcaegen2.platform.mod.web;
24
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;
39
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;
63
64 /**
65  * @author
66  * @date 09/22/2020
67  * Mock Test cases for AuthenticationController
68  */
69 @WebMvcTest(AuthController.class)
70 public class AuthControllerTest {
71
72     @Autowired
73     private MockMvc mockMvc;
74
75     @MockBean
76     JwtUtils jwtUtils;
77
78     @MockBean
79     AuthenticationManager authenticationManager;
80
81     @MockBean
82     UserRepository userRepository;
83
84     @MockBean
85     RoleRepository roleRepository;
86
87     @MockBean
88     PasswordEncoder passwordEncoder;
89
90     @MockBean
91     UserDetailsServiceImpl userDetailsService;
92
93     @MockBean
94     AuthEntryPointJwt authEntryPointJwt;
95
96     @Mock
97     Authentication authentication;
98
99     @BeforeEach
100     void setUp() {
101     }
102
103
104     @Test
105     void test_signin_returnsSuccessResponse() throws Exception {
106
107         LoginRequest loginRequest = getLoginRequest();
108
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");
112
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());
118
119         verify(authenticationManager, times(1)).authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
120         verify(jwtUtils, times(1)).generateJwtToken(any());
121     }
122
123
124     @WithMockUser(roles="ADMIN")
125     @Test
126     void test_signup_returnsSuccessResponse() throws Exception {
127
128         SignupRequest signUpRequest = getSignupRequest();
129
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());
134
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());
140
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());
145     }
146
147
148 }