All files / src/tools/auth0/handlers guardianPolicies.ts

95% Statements 19/20
90% Branches 9/10
100% Functions 3/3
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 581x 1x     1x                           1x           134x                 6x 1x     5x 5x 5x 5x         2x     2x   1x 1x 1x 1x 1x      
import DefaultHandler from './default';
import constants from '../../constants';
import { Asset, Assets } from '../../../types';
 
export const schema = {
  type: 'object',
  properties: {
    policies: {
      type: 'array',
      items: {
        type: 'string',
        enum: constants.GUARDIAN_POLICIES,
      },
    },
  },
  additionalProperties: false,
};
 
export default class GuardianPoliciesHandler extends DefaultHandler {
  existing: {
    policies: Asset[];
  };
 
  constructor(options) {
    super({
      ...options,
      type: 'guardianPolicies',
    });
  }
 
  //TODO: standardize empty object literal with more intentional empty indicator
  async getType(): Promise<GuardianPoliciesHandler['existing'] | {}> {
    // in case client version does not support the operation
    if (!this.client.guardian || typeof this.client.guardian.getPolicies !== 'function') {
      return {};
    }
 
    Iif (this.existing) return this.existing;
    const policies = await this.client.guardian.getPolicies();
    this.existing = { policies };
    return this.existing;
  }
 
  async processChanges(assets: Assets): Promise<void> {
    // No API to delete or create guardianPolicies, we can only update.
    const { guardianPolicies } = assets;
 
    // Do nothing if not set
    if (!guardianPolicies || !guardianPolicies.policies) return;
 
    const params = {};
    const data = guardianPolicies.policies;
    await this.client.guardian.updatePolicies(params, data);
    this.updated += 1;
    this.didUpdate(guardianPolicies);
  }
}