Building Tests for AdminGroups


#1

There are many ways to test AdminGroup functionality. Here’s an example of a good, simple way to test AdminGroups that you’ve created to support end users of your applications on the C3 Platform. Please note, this test is only a simple example and may not cover all use cases, but it should get you start in the right direction, and you can add edge cases for your specific application logic as required.

var filename = "test_NameOfAdminGroup";
describe(filename, function () {
  var ctx, allowedPermissionsMap;

  it("setup context", function () {
    ctx = TestApi.createContext(filename);
    allowedPermissionsMap = {
      "TypeName": ["actionsOnType"]  // example: "Unit": ["fetch", "evaluate"]
    }
  });

  it("checks that the AdminGroup is allowed to call TypeName.actionsOnType", function() {
   var contextUsername = c3Context().username; // grabs the username of the context
    User.get(contextUsername).impersonateGroup("AdminGroup"); // impersonates the AdminGroup for the purposes of the test.
    try {
      TestApi.expectAllowed(ctx, allowedFullEnterpriseUser); // this `expectAllowed` is a method on the TestApi type. c3ShowType(TestApi) for more details.

  // note that there is also an `expectDenied` method with works in the same way, that would take in a similar map
  // of deniedPermissionsMap, if we were interested in testing that a user does NOT have access to certain actions on types.

    } finally {  // lastly, we want to unimpersonate the AdminGroup.
      User.get(contextUsername).unimpersonateGroup();
    }
  });

  afterAll(function () {
    TestApi.teardown(ctx);
  });
});