import { ConfigPolicy, PolicyFromServer } from '../policy'; import Preferences from '../preferences'; import XHRRetry from '../../common/xhrRetry'; jest.mock('../../common/xhrRetry'); jest.mock('../preferences'); beforeEach(() => { //@ts-ignore chrome.storage.managed.get.mockImplementation((callback) => { callback({}); }); //@ts-ignore chrome.runtime.getManifest.mockImplementation(() => ({ policy_url: 'https://api-lsa-dev1.lenovosoftware.com/0/lsa/lanschool/clientInstaller/chrome/policy/' })); }); describe('PolicyFromServer', () => { it('should translate the policy from Google form into a schema we can use', () => { let policyString = '{"enabled":{"Value":true},"full_screen_thumbnail":{"Value":false},"provisioning_code":{"Value":"e6772b7e-e9a0-46da-aa00-ad543b01294b"},"api_server":{"Value":"https://api-lsa-dev.lenovosoftware.com"},"reprovision_code":{}}'; let p = new PolicyFromServer('nothing', 'nothing'); let o = JSON.parse(policyString); let sanitizedObj = p.sanitizePolicy(o); expect(sanitizedObj).toEqual({ api_server: 'https://api-lsa-dev.lenovosoftware.com', enabled: true, full_screen_thumbnail: false, provisioning_code: 'e6772b7e-e9a0-46da-aa00-ad543b01294b', reprovision_code: {} }); }); it('should not change anything about the policy schema since it is already in the right form', () => { let policyString = '{"enabled":true,"full_screen_thumbnail":false,"provisioning_code":"e6772b7e-e9a0-46da-aa00-ad543b01294b","api_server":"https://api-lsa-dev.lenovosoftware.com","reprovision_code":{}}'; let p = new PolicyFromServer('nothing', 'nothing'); let o = JSON.parse(policyString); let sanitizedObj = p.sanitizePolicy(o); expect(sanitizedObj).toEqual({ api_server: 'https://api-lsa-dev.lenovosoftware.com', enabled: true, full_screen_thumbnail: false, provisioning_code: 'e6772b7e-e9a0-46da-aa00-ad543b01294b', reprovision_code: {} }); }); it('should successfully retrieve policy from the server', async () => { //@ts-ignore jest.spyOn(XHRRetry.prototype, 'retrieve').mockImplementation(() => { return JSON.stringify({ this_is_a_good_object: true }); }); let p = new PolicyFromServer('https://api.policychecker.com/', 'abcdef'); let policy = await p.retrievePolicy(); expect(policy).toEqual({ this_is_a_good_object: true }); //FIXME - this check is identical to the call args. it seems like a jest bug that is causing it to fail //expect(XHRRetry).toHaveBeenCalledWith('https://api.policychecker.com/abcdef',{"key": "Content-type", "value": "application/json"}, {"maxAttempts": 5, "responseChecker": expect.any(Function), "retryPeriod": 10000}); }); }); describe('ConfigPolicy', function () { it('should load default policy if google or self-hosted are not available', async () => { jest.spyOn(XHRRetry.prototype, 'retrieve').mockImplementation(() => Promise.reject(400)); const mockPrefs = new Preferences(); const configPolicy = new ConfigPolicy(mockPrefs); let policy = await configPolicy.loadPolicy(); expect(policy).toEqual({ api_server: 'https://api-lsa.lenovosoftware.com', enabled: true, full_screen_thumbnail: false, provisioning_code: 'deadbeef-4811-30f6-42e7-7d8421b2bece', reprovision_code: '1000', student_privacy: false, allowNonChromebook: undefined }); }); it('should load the self hosted policy if the google one is not available', async () => { //@ts-ignore jest.spyOn(XHRRetry.prototype, 'retrieve').mockImplementation(() => { return JSON.stringify({ api_server: 'https://api-lsa-dev2.lenovosoftware.com', enabled: true, provisioning_code: 'deadbeef-4811-30f6-42e7-7d8421b2bece', reprovision_code: '1000', socket_server: 'https://socket.api-lsa.lenovosoftware.com/', student_privacy: false }); }); const mockPrefs = new Preferences(); const configPolicy = new ConfigPolicy(mockPrefs); let policy = await configPolicy.loadPolicy(); expect(policy).toEqual({ api_server: 'https://api-lsa-dev2.lenovosoftware.com', enabled: true, full_screen_thumbnail: false, provisioning_code: 'deadbeef-4811-30f6-42e7-7d8421b2bece', reprovision_code: '1000', socket_server: 'https://socket.api-lsa.lenovosoftware.com/', student_privacy: false }); }); it('should load the google hosted policy when available', async () => { //@ts-ignore chrome.storage.managed.get.mockImplementation((callback) => { callback({ api_server: 'https://api-lsa-dev1.lenovosoftware.com' }); }); const mockPrefs = new Preferences(); const configPolicy = new ConfigPolicy(mockPrefs); let policy = await configPolicy.loadPolicy(); expect(policy).toEqual({ api_server: 'https://api-lsa-dev1.lenovosoftware.com', enabled: true, full_screen_thumbnail: false, provisioning_code: 'deadbeef-4811-30f6-42e7-7d8421b2bece', reprovision_code: '1000', student_privacy: false }); }); });