Back to Repositories

Testing Consumer Role Permission Validation in Apollo Config

This test suite validates the consumer role permission functionality in Apollo’s OpenAPI service, focusing on permission verification and access control mechanisms. The tests ensure proper handling of consumer permissions across different target IDs and permission types.

Test Coverage Overview

The test suite provides comprehensive coverage of the ConsumerRolePermissionService’s permission verification system.

  • Tests consumer permission validation across different target IDs
  • Verifies multiple permission types for different consumers
  • Includes negative testing for consumers without permissions
  • Validates permission combinations and access control logic

Implementation Analysis

The testing approach utilizes Spring’s SQL script injection for test data setup and cleanup.

Key implementation patterns include:
  • Database state management through @Sql annotations
  • Integration with AbstractIntegrationTest base class
  • Systematic permission verification using assertTrue/assertFalse assertions
  • Clear separation of test data preparation and verification logic

Technical Details

Testing infrastructure includes:

  • JUnit 4 testing framework
  • Spring Test context for dependency injection
  • SQL scripts for test data management
  • Automated cleanup mechanisms
  • Integration test configuration for database interaction

Best Practices Demonstrated

The test implementation showcases several testing best practices.

  • Proper test data isolation using SQL scripts
  • Clear test method naming conventions
  • Comprehensive positive and negative test scenarios
  • Efficient test setup and teardown management
  • Well-structured test organization with @Before setup

apolloconfig/apollo

apollo-portal/src/test/java/com/ctrip/framework/apollo/openapi/service/ConsumerRolePermissionServiceTest.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.openapi.service;

import com.ctrip.framework.apollo.portal.AbstractIntegrationTest;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.jdbc.Sql;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * @author Jason Song([email protected])
 */
public class ConsumerRolePermissionServiceTest extends AbstractIntegrationTest {
  @Autowired
  private ConsumerRolePermissionService consumerRolePermissionService;

  @Before
  public void setUp() throws Exception {

  }

  @Test
  @Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/permission/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/permission/insert-test-consumerroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/permission/insert-test-rolepermissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testConsumerHasPermission() throws Exception {
    String someTargetId = "someTargetId";
    String anotherTargetId = "anotherTargetId";
    String somePermissionType = "somePermissionType";
    String anotherPermissionType = "anotherPermissionType";
    long someConsumerId = 1;
    long anotherConsumerId = 2;
    long someConsumerWithNoPermission = 3;

    assertTrue(consumerRolePermissionService.consumerHasPermission(someConsumerId, somePermissionType, someTargetId));
    assertTrue(consumerRolePermissionService.consumerHasPermission(someConsumerId, anotherPermissionType, anotherTargetId));
    assertTrue(consumerRolePermissionService.consumerHasPermission(anotherConsumerId, somePermissionType, someTargetId));
    assertTrue(consumerRolePermissionService.consumerHasPermission(anotherConsumerId, anotherPermissionType, anotherTargetId));

    assertFalse(consumerRolePermissionService.consumerHasPermission(someConsumerWithNoPermission, somePermissionType, someTargetId));
    assertFalse(consumerRolePermissionService.consumerHasPermission(someConsumerWithNoPermission, anotherPermissionType, anotherTargetId));

  }

}