Test

The surface of an Elide web service can become quite large given the numerous ways of navigating a complex data model. This can make testing daunting.Elide has a sibling project for testing the authorization logic of your service.

Overview

The test framework allows the developer to redefine the authorization rules for different classes of users in a domain specific language.

Provided some test data for a DataStore, the framework builds a graph (cycles removed) of every possibleway to navigate the test data originating from the rootable entities.

For every collection and entity in the graph, it tests all combinations of CRUD operations - comparing the result returned from Elide with the expected resultdefined in the domain specific language. Any mismatches are reported as test failures.

Domain Specific Language

The DSL is structured as a gherkin feature file with the following elements:

Exposed Entities

Exposed entities is a table describing which JPA entities are exposed through Elide and which of those entities are rootable.

  1. Given exposed entities
  2. | EntityName | Rootable |
  3. | parent | true |
  4. | child | false |

Users

There will be different classes of users who have access to the system. A class of users are the set of users who share the same authorizationpermissions. Ideally, the developer should define a user for each permutation of authorization permissions they want to test.

  1. And users
  2. # Amalberti father
  3. | Emmanuel |
  4. # Bonham parents
  5. | Mo |
  6. | Margery |

The concept of users is opaque to the security test framework. It has no concept of authentication or user identity. Instead, the developer providesa concrete implementation of a UserFactory which constructs the user objects your authorization code expects:

  1. public interface UserFactory {
  2. User makeUser(String alias);
  3. }

Associated Permissions

Associated permissions is a table that defines which entities a given user class has access to. For each set of entities, it alsodefines which CRUD operations are allowed.

  1. And associated permissions
  2. | UserName | EntityName | ValidIdsList | EntityPermissions | RestrictedReadFields | RestrictedWriteFields |
  3. ########### ############ ############## ########################### ####################### #######################
  4. | Mo | parent | Mo | Create,Read,Update | | deceased |
  5. | Mo | parent | Mo's Spouse | Create,Read,Update | otherSpouses | deceased |
  6. | Mo | parent | Emmanuel,Goran,Hina | Read,Update | otherSpouses | [EXCLUDING] friends |

UserName

This column identifies the user class.

EntityName

This column identifies the JPA entity. The name must match the name exposed via Elide.

ValidIdsList

There is a grammar which defines the syntax for this column. In short, it can be one of the following:

  • A comma separated list of entity IDs. The IDs much match the test data in the DataStore.
  • The keyword [ALL] which signifies all the IDs found in the test data for the given entity.
  • An expression [type.collection] where type is another entity and collection is a relationship inside of type that contains elements of type EntityName. This expression semantically means: ‘If the user has any access to an entity of type type, they should have access to everything inside that entity’s collection collection.’ More concretely, this expression will get expanded to a comma separated list of IDs by first expanding type to the set of entities of type type the user class has access to. For each of these entities, it will then expand to the list of IDs contained within collection. Expressions of this type can be combined within lists of comma separated IDs.
    Aliases can also be defined to represent one or more comma separated IDs, These aliases are expanded in the grammar expression prior to parsing. Aliases are defined in the gherkin file:
  1. And aliases
  2. | Mo | 1 |
  3. | Margery | 2 |
  4. | Mo's Spouse | Margery |
  5. | Margery's Spouse | Mo |
  6. | Margery's Ex | Emmanuel |
  7. | Emmanuel | 3 |
  8. | Emmanuel's Ex | Margery |
  9. | Goran | 4 |
  10. | Bonham Children | 1,2,3 |
  11. | Amalberti Children | 4,5,6 |
  12. | Tang Children | 7 |

Aliases can reference other aliases.

EntityPermissions

Entity Permissions are the list of CRUD permissions that are allowed for the given list of entities for the given user. They should be definedas a comma separated list of the keywords ‘Create’, ‘Read’, ‘Update’, and ‘Delete’.

RestrictedReadFields & RestrictedWriteFields

The list of entity fields the given user class should not be able to read or write respectively.There is a grammar which defines the syntax for this column. In short, it can be one of the following:

  • A comma separated list of field names.
  • The keyword [ALL] which signifies all fields.
  • The keyword [EXCLUDING] followed by a list of fields. This inverts the column to a white list of allowed fields instead of a blacklist of excluded fields.

    Disabled Test Ids

  1. And disabled test ids
  2. | CreateRelation:child#5/relationships/playmates:Denied=[1,2,3] |
  3. | CreateRelation:child#6/relationships/playmates:Denied=[3] |

If you want to disable a test from running, you can do so by adding ‘name’ of the test to this list. The ‘name’ of the tests are displayedin the output of test framework executions.

Complete Example

A full example of the configuration DSL can be found here

Using The Framework

Using the framework can be broken down into the following steps:

  • Create a feature file using the described DSL.
  • Write a Java program that does the following:
    • Create a DataStore
    • Create test data.
    • Create a UserFactory.
    • Create a ValidationDriver and invoke execute.
  1. /* The data store we use is orthogonal to the correctness of security checks */
  2. DataStore dataStore = new InMemoryDB();
  3. /* Construct test data and persist to the store */
  4. initializeTestData(dataStore);
  5. String featureFile = "SampleConfig.feature";
  6. /* Pass in a NOOP logger */
  7. AuditLogger logger = new AuditLogger() {
  8. @Override
  9. public void commit() throws IOException {}
  10. };
  11. /* This class will instantiate our users */
  12. UserFactory userFactory = new MyUserFactory(dataStore);
  13. /* Create and execute the test driver */
  14. ValidationDriver driver = new ValidationDriver(featureFile, userFactory, dataStore, logger);
  15. /* Results per user can be printed or processed in some other way */
  16. Map<UserProfile, List<ValidationResult>> results = driver.execute();

Given that the goal of the test framework is limited to testing authorization code (security check logic), the DataStore used to furnish test datashould be orthogonal to the correctness of the tests.

Fail On Missing Tests

The ValidationDriver takes an optional boolean parameter (failOnMissingTests) that will fail the test execution if there is no test datafor any entity exposed via Elide. If this parameter is not set, it will simply log the absence of test data.

原文: http://elide.io/pages/guide/14-test.html