|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.api.auth; |
| 18 | + |
| 19 | +import java.net.InetAddress; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import javax.servlet.http.HttpServletRequest; |
| 24 | +import javax.servlet.http.HttpServletResponse; |
| 25 | +import javax.servlet.http.HttpSession; |
| 26 | + |
| 27 | +import org.apache.cloudstack.api.ApiConstants; |
| 28 | +import org.apache.cloudstack.api.ApiServerService; |
| 29 | +import org.apache.cloudstack.api.ResponseObject; |
| 30 | +import org.apache.cloudstack.resourcedetail.UserDetailVO; |
| 31 | +import org.apache.cloudstack.resourcedetail.dao.UserDetailsDao; |
| 32 | +import org.junit.After; |
| 33 | +import org.junit.Assert; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | +import org.mockito.Mock; |
| 38 | +import org.mockito.MockedStatic; |
| 39 | +import org.mockito.Mockito; |
| 40 | +import org.mockito.junit.MockitoJUnitRunner; |
| 41 | + |
| 42 | +import com.cloud.api.response.ApiResponseSerializer; |
| 43 | +import com.cloud.domain.Domain; |
| 44 | +import com.cloud.user.AccountService; |
| 45 | +import com.cloud.user.DomainService; |
| 46 | +import com.cloud.user.UserAccount; |
| 47 | + |
| 48 | +@RunWith(MockitoJUnitRunner.class) |
| 49 | +public class DefaultLoginAPIAuthenticatorCmdTest { |
| 50 | + |
| 51 | + private static final String USERNAME = "user@example.com"; |
| 52 | + private static final long DOMAIN_ID = 1L; |
| 53 | + private static final long USER_ID = 42L; |
| 54 | + private static final String SERIALIZED_RESPONSE = "{\"loginresponse\":{}}"; |
| 55 | + |
| 56 | + @Mock |
| 57 | + ApiServerService apiServer; |
| 58 | + @Mock |
| 59 | + UserDetailsDao userDetailsDao; |
| 60 | + @Mock |
| 61 | + AccountService accountService; |
| 62 | + @Mock |
| 63 | + DomainService domainService; |
| 64 | + @Mock |
| 65 | + HttpSession session; |
| 66 | + @Mock |
| 67 | + HttpServletRequest req; |
| 68 | + @Mock |
| 69 | + HttpServletResponse resp; |
| 70 | + @Mock |
| 71 | + Domain domain; |
| 72 | + @Mock |
| 73 | + ResponseObject loginResponse; |
| 74 | + |
| 75 | + DefaultLoginAPIAuthenticatorCmd cmd = new DefaultLoginAPIAuthenticatorCmd(); |
| 76 | + |
| 77 | + private MockedStatic<ApiResponseSerializer> serializerMock; |
| 78 | + private Map<String, Object[]> params; |
| 79 | + |
| 80 | + @Before |
| 81 | + public void setUp() throws Exception { |
| 82 | + cmd._apiServer = apiServer; |
| 83 | + cmd._accountService = accountService; |
| 84 | + cmd._domainService = domainService; |
| 85 | + cmd.userDetailsDao = userDetailsDao; |
| 86 | + |
| 87 | + params = new HashMap<>(); |
| 88 | + params.put(ApiConstants.USERNAME, new String[] {USERNAME}); |
| 89 | + params.put(ApiConstants.PASSWORD, new String[] {"password"}); |
| 90 | + params.put(ApiConstants.DOMAIN, new String[] {"/"}); |
| 91 | + |
| 92 | + Mockito.when(req.getMethod()).thenReturn("POST"); |
| 93 | + Mockito.when(domain.getId()).thenReturn(DOMAIN_ID); |
| 94 | + Mockito.when(domainService.findDomainByIdOrPath(Mockito.any(), Mockito.anyString())).thenReturn(domain); |
| 95 | + Mockito.when(apiServer.loginUser(Mockito.eq(session), Mockito.eq(USERNAME), Mockito.anyString(), Mockito.eq(DOMAIN_ID), |
| 96 | + Mockito.anyString(), Mockito.any(), Mockito.any())).thenReturn(loginResponse); |
| 97 | + Mockito.when(session.getAttribute("userid")).thenReturn(USER_ID); |
| 98 | + |
| 99 | + serializerMock = Mockito.mockStatic(ApiResponseSerializer.class); |
| 100 | + serializerMock.when(() -> ApiResponseSerializer.toSerializedString(loginResponse, "json")).thenReturn(SERIALIZED_RESPONSE); |
| 101 | + } |
| 102 | + |
| 103 | + @After |
| 104 | + public void tearDown() { |
| 105 | + serializerMock.close(); |
| 106 | + } |
| 107 | + |
| 108 | + private String authenticate() throws Exception { |
| 109 | + return cmd.authenticate("login", params, session, InetAddress.getLoopbackAddress(), "json", new StringBuilder(), req, resp); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void authenticateUserCreatedDuringLoginReturnsLoginResponse() throws Exception { |
| 114 | + Mockito.when(accountService.getActiveUserAccount(USERNAME, DOMAIN_ID)).thenReturn(null); |
| 115 | + |
| 116 | + Assert.assertEquals(SERIALIZED_RESPONSE, authenticate()); |
| 117 | + Mockito.verify(userDetailsDao).removeDetail(USER_ID, UserDetailVO.OauthLogin); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void authenticateExistingUserRemovesOauthLoginDetail() throws Exception { |
| 122 | + UserAccount userAccount = Mockito.mock(UserAccount.class); |
| 123 | + Mockito.when(accountService.getActiveUserAccount(USERNAME, DOMAIN_ID)).thenReturn(userAccount); |
| 124 | + |
| 125 | + Assert.assertEquals(SERIALIZED_RESPONSE, authenticate()); |
| 126 | + Mockito.verify(userDetailsDao).removeDetail(USER_ID, UserDetailVO.OauthLogin); |
| 127 | + } |
| 128 | +} |
0 commit comments