google-nomulus/javatests/google/registry/tools/server/CreateGroupsActionTest.java
mcilwain b825a2b5a8 Get rid of custom ExceptionRule methods
The only remaining methods on ExceptionRule after this are methods that
also exist on ExpectedException, which will allow us to, in the next CL,
swap out the one for the other and then run the automated refactoring to
turn it all into assertThrows/expectThrows.

Note that there were some assertions about root causes that couldn't
easily be turned into ExpectedException invocations, so I simply
converted them directly to usages of assertThrows/expectThrows.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178623431
2017-12-13 12:43:45 -05:00

148 lines
6.5 KiB
Java

// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.tools.server;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import google.registry.groups.DirectoryGroupsConnection;
import google.registry.groups.GroupsConnection.Role;
import google.registry.request.HttpException.BadRequestException;
import google.registry.request.HttpException.InternalServerErrorException;
import google.registry.request.Response;
import google.registry.testing.AppEngineRule;
import google.registry.testing.ExceptionRule;
import google.registry.testing.InjectRule;
import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Unit tests for {@link CreateGroupsAction}.
*/
@RunWith(JUnit4.class)
public class CreateGroupsActionTest {
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder()
.withDatastore()
.build();
@Rule
public final ExceptionRule thrown = new ExceptionRule();
@Rule
public final InjectRule inject = new InjectRule();
private final DirectoryGroupsConnection connection = mock(DirectoryGroupsConnection.class);
private final Response response = mock(Response.class);
private void runAction(String clientId) {
CreateGroupsAction action = new CreateGroupsAction();
action.response = response;
action.groupsConnection = connection;
action.gSuiteDomainName = "domain-registry.example";
action.clientId = Optional.ofNullable(clientId);
action.run();
}
@Test
public void test_invalidRequest_missingClientId() throws Exception {
thrown.expect(BadRequestException.class);
thrown.expectMessage("Error creating Google Groups, missing parameter: clientId");
runAction(null);
}
@Test
public void test_invalidRequest_invalidClientId() throws Exception {
thrown.expect(BadRequestException.class);
thrown.expectMessage(
"Error creating Google Groups; could not find registrar with id completelyMadeUpClientId");
runAction("completelyMadeUpClientId");
}
@Test
public void test_createsAllGroupsSuccessfully() throws Exception {
runAction("NewRegistrar");
verify(response).setStatus(SC_OK);
verify(response).setPayload("Success!");
verifyGroupCreationCallsForNewRegistrar();
verify(connection).addMemberToGroup("registrar-primary-contacts@domain-registry.example",
"newregistrar-primary-contacts@domain-registry.example",
Role.MEMBER);
}
@Test
public void test_createsSomeGroupsSuccessfully_whenOthersFail() throws Exception {
when(connection.createGroup("newregistrar-primary-contacts@domain-registry.example"))
.thenThrow(new RuntimeException("Could not contact server."));
doThrow(new RuntimeException("Invalid access.")).when(connection).addMemberToGroup(
"registrar-technical-contacts@domain-registry.example",
"newregistrar-technical-contacts@domain-registry.example",
Role.MEMBER);
InternalServerErrorException e =
expectThrows(InternalServerErrorException.class, () -> runAction("NewRegistrar"));
String responseString = e.toString();
assertThat(responseString).contains("abuse => Success");
assertThat(responseString).contains("billing => Success");
assertThat(responseString).contains("legal => Success");
assertThat(responseString).contains("marketing => Success");
assertThat(responseString).contains("whois-inquiry => Success");
assertThat(responseString).contains(
"primary => java.lang.RuntimeException: Could not contact server.");
assertThat(responseString).contains(
"technical => java.lang.RuntimeException: Invalid access.");
verifyGroupCreationCallsForNewRegistrar();
}
private void verifyGroupCreationCallsForNewRegistrar() throws Exception {
verify(connection).createGroup("newregistrar-abuse-contacts@domain-registry.example");
verify(connection).createGroup("newregistrar-primary-contacts@domain-registry.example");
verify(connection).createGroup("newregistrar-billing-contacts@domain-registry.example");
verify(connection).createGroup("newregistrar-legal-contacts@domain-registry.example");
verify(connection).createGroup("newregistrar-marketing-contacts@domain-registry.example");
verify(connection).createGroup("newregistrar-technical-contacts@domain-registry.example");
verify(connection).createGroup("newregistrar-whois-inquiry-contacts@domain-registry.example");
verify(connection).addMemberToGroup("registrar-abuse-contacts@domain-registry.example",
"newregistrar-abuse-contacts@domain-registry.example",
Role.MEMBER);
// Note that addMemberToGroup for primary is verified separately for the success test because
// the exception thrown on group creation in the failure test causes the servlet not to get to
// this line.
verify(connection).addMemberToGroup("registrar-billing-contacts@domain-registry.example",
"newregistrar-billing-contacts@domain-registry.example",
Role.MEMBER);
verify(connection).addMemberToGroup("registrar-legal-contacts@domain-registry.example",
"newregistrar-legal-contacts@domain-registry.example",
Role.MEMBER);
verify(connection).addMemberToGroup("registrar-marketing-contacts@domain-registry.example",
"newregistrar-marketing-contacts@domain-registry.example",
Role.MEMBER);
verify(connection).addMemberToGroup("registrar-technical-contacts@domain-registry.example",
"newregistrar-technical-contacts@domain-registry.example",
Role.MEMBER);
verify(connection).addMemberToGroup(
"registrar-whois-inquiry-contacts@domain-registry.example",
"newregistrar-whois-inquiry-contacts@domain-registry.example",
Role.MEMBER);
}
}