Skip to content

Tutorial 6

PhuocLe edited this page Aug 26, 2018 · 5 revisions

Task

  • Lead can qualify by a user who has security role: Qualify Lead

Prerequisites

Coding

  1. Open file Lead.js in the folder entities of the project Paz.LuckeyMonkey.WebResource
  2. Add function hasRole and return HasRole. Please DON'T copy/paste code. Try typing to get intellisense that PL.DynamicsCrm.DevKit support. And remember to use View-FetchXML to copy/paste Javascript code
...
    function hasRole(role) {
	    var fetchData = {
	        name: role
	    };
	    var fetchXml = [
    "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>",
    "  <entity name='systemuser'>",
    "    <attribute name='systemuserid'/>",
    "    <filter type='and'>",
    "      <condition attribute='systemuserid' operator='eq-userid'/>",
    "    </filter>",
    "    <link-entity name='systemuserroles' from='systemuserid' to='systemuserid' visible='false' intersect='true'>",
    "      <link-entity name='role' from='roleid' to='roleid' alias='ae'>",
    "        <filter type='and'>",
    "          <condition attribute='name' operator='eq' value='", fetchData.name, "'/>",
    "        </filter>",
    "      </link-entity>",
    "    </link-entity>",
    "  </entity>",
    "</fetch>",
	    ].join("");
	    var req = new LuckeyMonkey.WebApi.RetrieveRequest();
	    req.async = false;
	    req.fetchXml = fetchXml;
	    req.entityName = "systemuser";
	    var res = WebApiClient.Retrieve(req);
	    if (res.value.length === 0) return false;
	    return true;
	}
	return {
		OnLoad: onLoad,
		OnSave: onSave,
                HasRole: hasRole,
		__SubjectAddOnChange__: SubjectAddOnChange,
		__PhoneAddOnChange__: PhoneAddOnChange
	};
...
  1. Open file Lead.test.js in the folder test of the project Paz.LuckeyMonkey.WebResource
  2. Add these test method bellow
...
        it("formLead.HasRole('Qualify Lead')", function () {
            //setup
            var data = {
                "@odata.context": "http://fakeurl.com/api/data/v8.0/$metadata#systemusers(systemuserid,ownerid)",
                "value": [{
                    "@odata.etag": "W/\"320689\"",
                    "systemuserid": "59046ca1-5579-e811-80dc-000d3aa2cf16",
                    "ownerid": "59046ca1-5579-e811-80dc-000d3aa2cf16"
                }]
            };
            server.RespondWith("Qualify Lead", data);
            //call
            var hasRole = formLead.HasRole("Qualify Lead");
            //result
            expect(hasRole).toBe(true);
        });
        it("formLead.HasRole('System Administrator')", function () {
            var data = {
                "@odata.context": "http://fakeurl.com/api/data/v8.0/$metadata#systemusers(systemuserid,ownerid)",
                "value": []
            };
            server.RespondWith("System Administrator", data);
            //call
            var hasRole = formLead.HasRole("System Administrator");
            //result
            expect(hasRole).toBe(false);
        });
...
  1. Run JS Tests and you get the result bellow
------ Test started: File: C:\src\github\Dynamics-Crm-DevKit\tutorials\Paz.LuckeyMonkey.WebResource\test\Lead.test.js ------
4 passed, 0 failed, 4 total (chutzpah).

========== Total Tests: 4 passed, 0 failed, 4 total ==========
  1. Check-in all files to your source control
  2. You finished this tutorial

Summary

This tutorial, you know howto

  • Use library WebApiClient
  • Unit test Javascript with sinon
Clone this wiki locally