Back to Repositories

Validating User Management Controller Implementation in Apollo Config

This test suite validates the user management functionality in Apollo’s portal component, focusing on user creation and password validation. It ensures robust user authentication and data handling through comprehensive unit testing.

Test Coverage Overview

The test suite provides coverage for the UserInfoController class, specifically targeting user creation and update operations. Key functionality tested includes:

  • Successful user creation with valid credentials
  • Password strength validation
  • Error handling for weak passwords
  • Integration with SpringSecurityUserService

Implementation Analysis

The testing approach utilizes Mockito for dependency isolation and JUnit for test execution. The implementation employs the MockitoJUnitRunner to handle mock injection and verification. The tests demonstrate proper use of mock behaviors and exception handling patterns specific to Apollo’s user management system.

Technical Details

Testing tools and configuration include:

  • JUnit 4 test framework
  • Mockito mocking framework
  • MockitoJUnitRunner for test execution
  • InjectMocks and Mock annotations for dependency management
  • Assert statements for verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper separation of concerns with mock dependencies
  • Explicit test case naming
  • Comprehensive exception testing
  • Clear verification of error messages
  • Focused test methods with single responsibilities

apolloconfig/apollo

apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/controller/UserInfoControllerTest.java

            
/*
 * Copyright 2024 Apollo Authors
 *
 * 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 com.ctrip.framework.apollo.portal.controller;

import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.portal.entity.po.UserPO;
import com.ctrip.framework.apollo.portal.spi.springsecurity.SpringSecurityUserService;
import com.ctrip.framework.apollo.portal.util.checker.AuthUserPasswordChecker;
import com.ctrip.framework.apollo.portal.util.checker.CheckResult;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class UserInfoControllerTest {

  @InjectMocks
  private UserInfoController userInfoController;
  @Mock
  private SpringSecurityUserService userService;
  @Mock
  private AuthUserPasswordChecker userPasswordChecker;

  @Test
  public void testCreateOrUpdateUser() {
    UserPO user = new UserPO();
    user.setUsername("username");
    user.setPassword("password");

    Mockito.when(userPasswordChecker.checkWeakPassword(Mockito.anyString()))
        .thenReturn(new CheckResult(Boolean.TRUE, ""));

    userInfoController.createOrUpdateUser(true, user);
  }

  @Test(expected = BadRequestException.class)
  public void testCreateOrUpdateUserFailed() {
    UserPO user = new UserPO();
    user.setUsername("username");
    user.setPassword("password");

    String msg = "fake error message";

    Mockito.when(userPasswordChecker.checkWeakPassword(Mockito.anyString()))
        .thenReturn(new CheckResult(Boolean.FALSE, msg));

    try {
      userInfoController.createOrUpdateUser(true, user);
    } catch (BadRequestException e) {
      Assert.assertEquals(msg, e.getMessage());
      throw e;
    }
  }

}