Skip to content
Snippets Groups Projects
Commit 44b736d9 authored by Wyatt Nielsen's avatar Wyatt Nielsen
Browse files

Fix duplicate AuthorizationService.java and missing entitlements bean from is-core

parent 72d9453a
No related branches found
No related tags found
1 merge request!6Trusted ibm
// Copyright 2017-2019, Schlumberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.indexer.auth;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.opengroup.osdu.core.common.model.entitlements.EntitlementsException;
import org.opengroup.osdu.core.common.service.entitlements.IEntitlementsFactory;
import org.opengroup.osdu.core.common.service.entitlements.IEntitlementsService;
import org.opengroup.osdu.core.common.model.entitlements.GroupInfo;
import org.opengroup.osdu.core.common.model.entitlements.Groups;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.service.http.HttpResponse;
import org.opengroup.osdu.core.common.model.entitlements.AuthorizationResponse;
import org.opengroup.osdu.core.common.service.logging.JaxRsDpsLog;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
@RunWith(SpringRunner.class)
public class AuthorizationServiceEntitlementsTest {
@Mock
private IEntitlementsFactory entitlementsFactory;
@Mock
private IEntitlementsService service;
@Mock
private JaxRsDpsLog log;
@InjectMocks
private AuthorizationServiceEntitlements sut;
@Before
public void setup() {
when(entitlementsFactory.create(any())).thenReturn(service);
}
@Test
public void should_returnUser_when_ussrHasPermission() throws EntitlementsException {
sut = createSut("service.search.user");
AuthorizationResponse result = sut.authorizeAny(DpsHeaders.createFromMap(new HashMap<>()), "service.search.user");
assertEquals("iamtester@dps.com", result.getUser());
}
@Test
public void should_returnUser_when_ussrHasAnyPermission() throws EntitlementsException {
sut = createSut("service.search.user");
AuthorizationResponse result = sut.authorizeAny(DpsHeaders.createFromMap(new HashMap<>()), "service.search.user", "service.search.owner");
assertEquals("iamtester@dps.com", result.getUser());
}
@Test
public void should_throwUnauthorized_when_userDoesNotHaveRequiredPermission() throws EntitlementsException {
sut = createSut("service.search.user");
try {
sut.authorizeAny(DpsHeaders.createFromMap(new HashMap<>()), "service.search.owner");
fail("expected exception");
} catch (AppException ex) {
assertEquals(401, ex.getError().getCode());
}
}
@Test
public void should_throwUnauthorized_when_userDoesNotBelongToAnyGroup() throws EntitlementsException {
sut = createSut("service.search.user");
HttpResponse response = new HttpResponse();
response.setResponseCode(401);
when(service.getGroups()).thenThrow(new EntitlementsException("", response));
try {
sut.authorizeAny(DpsHeaders.createFromMap(new HashMap<>()), "service.search.owner");
fail("expected exception");
} catch (AppException ex) {
assertEquals(401, ex.getError().getCode());
}
}
@Test
public void should_throwServerError_when_getGroupsThrowsServerError() throws EntitlementsException {
sut = createSut("service.search.user");
HttpResponse response = new HttpResponse();
response.setResponseCode(500);
when(service.getGroups()).thenThrow(new EntitlementsException("", response));
try {
sut.authorizeAny(DpsHeaders.createFromMap(new HashMap<>()), "service.search.owner");
fail("expected exception");
} catch (AppException ex) {
assertEquals(500, ex.getError().getCode());
}
}
@Test
public void should_throw403AppError_when_getGroupsThrows400EntitlementsError() throws EntitlementsException {
sut = createSut("service.search.user");
HttpResponse response = new HttpResponse();
response.setResponseCode(403);
when(service.getGroups()).thenThrow(new EntitlementsException("", response));
try {
sut.authorizeAny(DpsHeaders.createFromMap(new HashMap<>()), "service.search.owner");
fail("expected exception");
} catch (AppException ex) {
assertEquals(403, ex.getError().getCode());
}
}
private AuthorizationServiceEntitlements createSut(String... roles) throws EntitlementsException {
List<GroupInfo> groupInfos = new ArrayList<>();
for (String s : roles) {
GroupInfo group = new GroupInfo();
group.setName(s);
groupInfos.add(group);
}
Groups output = new Groups();
output.setMemberEmail("iamtester@dps.com");
output.setGroups(groupInfos);
when(service.getGroups()).thenReturn(output);
return sut;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment