Skip to content

Commit

Permalink
Test Refactor (PasswordChangeEventPublisherTests)
Browse files Browse the repository at this point in the history
- Apply Intellij suggestions and autoformat

[#164177717]

Signed-off-by: Joshua Casey <jcasey@pivotal.io>
Co-authored-by: Joshua Casey <jcasey@pivotal.io>
  • Loading branch information
2 people authored and Jeremy Morony committed Mar 27, 2019
1 parent 3a7749a commit f72fb61
Showing 1 changed file with 13 additions and 25 deletions.
@@ -1,16 +1,3 @@
/*******************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2017] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
*******************************************************************************/

package org.cloudfoundry.identity.uaa.account.event;

import org.cloudfoundry.identity.uaa.account.UaaPasswordTestFactory;
Expand All @@ -30,9 +17,10 @@
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import java.util.Arrays;
import java.util.Collections;

import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.when;

public class PasswordChangeEventPublisherTests {

Expand All @@ -47,9 +35,9 @@ public class PasswordChangeEventPublisherTests {
public void init() {
subject.setApplicationEventPublisher(publisher);
authentication = new OAuth2Authentication(
new AuthorizationRequest(
"client",
Arrays.asList("read")).createOAuth2Request(),
new AuthorizationRequest(
"client",
Collections.singletonList("read")).createOAuth2Request(),
UaaPasswordTestFactory.getAuthentication("ID", "joe", "joe@test.org")
);
SecurityContextHolder.getContext().setAuthentication(authentication);
Expand All @@ -62,37 +50,37 @@ public void destroy() {

@Test
public void testPasswordChange() {
Mockito.when(scimUserProvisioning.retrieve("foo", IdentityZoneHolder.get().getId())).thenReturn(
ScimUserTestFactory.getScimUser("joe", "joe@test.org", "Joe", "Schmo"));
when(scimUserProvisioning.retrieve("foo", IdentityZoneHolder.get().getId())).thenReturn(
ScimUserTestFactory.getScimUser("joe", "joe@test.org", "Joe", "Schmo"));
subject.passwordChange("foo");
Mockito.verify(publisher).publishEvent(ArgumentMatchers.isA(PasswordChangeEvent.class));
}

@Test
public void testPasswordChangeNoEmail() {
Mockito.when(scimUserProvisioning.retrieve("foo", IdentityZoneHolder.get().getId())).thenReturn(
ScimUserTestFactory.getScimUser("joe", null, "Joe", "Schmo"));
when(scimUserProvisioning.retrieve("foo", IdentityZoneHolder.get().getId())).thenReturn(
ScimUserTestFactory.getScimUser("joe", null, "Joe", "Schmo"));
subject.passwordChange("foo");
Mockito.verify(publisher).publishEvent(ArgumentMatchers.isA(PasswordChangeEvent.class));
}

@Test
public void testPasswordFailure() {
Mockito.when(scimUserProvisioning.retrieve("foo", IdentityZoneHolder.get().getId())).thenReturn(
ScimUserTestFactory.getScimUser("joe", "joe@test.org", "Joe", "Schmo"));
when(scimUserProvisioning.retrieve("foo", IdentityZoneHolder.get().getId())).thenReturn(
ScimUserTestFactory.getScimUser("joe", "joe@test.org", "Joe", "Schmo"));
subject.passwordFailure("foo", new RuntimeException("planned"));
Mockito.verify(publisher).publishEvent(ArgumentMatchers.isA(PasswordChangeFailureEvent.class));
}

@Test
public void testPasswordFailureNoUser() {
Mockito.when(scimUserProvisioning.retrieve("foo", IdentityZoneHolder.get().getId())).thenThrow(new ScimResourceNotFoundException("Not found"));
when(scimUserProvisioning.retrieve("foo", IdentityZoneHolder.get().getId())).thenThrow(new ScimResourceNotFoundException("Not found"));
subject.passwordFailure("foo", new RuntimeException("planned"));
Mockito.verify(publisher).publishEvent(ArgumentMatchers.any(PasswordChangeFailureEvent.class));
}

@Test
public void not_authenticated_returns_system_auth() throws Exception {
public void notAuthenticatedReturnsSystemAuth() {
assertSame(authentication, subject.getPrincipal());
SecurityContextHolder.clearContext();
assertSame(SystemAuthentication.SYSTEM_AUTHENTICATION, subject.getPrincipal());
Expand Down

0 comments on commit f72fb61

Please sign in to comment.