Welcome to mirror list, hosted at ThFree Co, Russian Federation.

source_editor_security_policy_schema_ext_spec.js « editor « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 96c876b27c90bb6c6d6d332a8b19b48236514fec (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import MockAdapter from 'axios-mock-adapter';
import { registerSchema } from '~/ide/utils';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
import { TEST_HOST } from 'helpers/test_constants';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import {
  getSecurityPolicyListUrl,
  getSecurityPolicySchemaUrl,
  getSinglePolicySchema,
  SecurityPolicySchemaExtension,
} from '~/editor/extensions/source_editor_security_policy_schema_ext';
import SourceEditor from '~/editor/source_editor';

jest.mock('~/ide/utils');

const mockNamespacePath = 'mock-namespace';

const mockSchema = {
  $id: 1,
  title: 'mockSchema',
  description: 'mockDescriptions',
  type: 'Object',
  properties: {
    scan_execution_policy: { items: { properties: { foo: 'bar' } } },
    scan_result_policy: { items: { properties: { fizz: 'buzz' } } },
  },
};

const createMockOutput = (policyType) => ({
  $id: mockSchema.$id,
  title: mockSchema.title,
  description: mockSchema.description,
  type: mockSchema.type,
  properties: {
    type: {
      type: 'string',
      description: 'Specifies the type of policy to be enforced.',
      enum: policyType,
    },
    ...mockSchema.properties[policyType].items.properties,
  },
});

describe('getSecurityPolicyListUrl', () => {
  it.each`
    input                                                     | output
    ${{ namespacePath: '' }}                                  | ${`${TEST_HOST}/groups/-/security/policies`}
    ${{ namespacePath: 'test', namespaceType: 'group' }}      | ${`${TEST_HOST}/groups/test/-/security/policies`}
    ${{ namespacePath: '', namespaceType: 'project' }}        | ${`${TEST_HOST}/-/security/policies`}
    ${{ namespacePath: 'test', namespaceType: 'project' }}    | ${`${TEST_HOST}/test/-/security/policies`}
    ${{ namespacePath: undefined, namespaceType: 'project' }} | ${`${TEST_HOST}/-/security/policies`}
    ${{ namespacePath: undefined, namespaceType: 'group' }}   | ${`${TEST_HOST}/groups/-/security/policies`}
    ${{ namespacePath: null, namespaceType: 'project' }}      | ${`${TEST_HOST}/-/security/policies`}
    ${{ namespacePath: null, namespaceType: 'group' }}        | ${`${TEST_HOST}/groups/-/security/policies`}
  `('returns `$output` when passed `$input`', ({ input, output }) => {
    expect(getSecurityPolicyListUrl(input)).toBe(output);
  });
});

describe('getSecurityPolicySchemaUrl', () => {
  it.each`
    namespacePath | namespaceType | output
    ${'test'}     | ${'project'}  | ${`${TEST_HOST}/test/-/security/policies/schema`}
    ${'test'}     | ${'group'}    | ${`${TEST_HOST}/groups/test/-/security/policies/schema`}
  `(
    'returns $output when passed $namespacePath and $namespaceType',
    ({ namespacePath, namespaceType, output }) => {
      expect(getSecurityPolicySchemaUrl({ namespacePath, namespaceType })).toBe(output);
    },
  );
});

describe('getSinglePolicySchema', () => {
  let mock;

  beforeEach(() => {
    mock = new MockAdapter(axios);
  });

  afterEach(() => {
    mock.restore();
  });

  it.each`
    policyType
    ${'scan_execution_policy'}
    ${'scan_result_policy'}
  `('returns the appropriate schema on request success for $policyType', async ({ policyType }) => {
    mock.onGet().reply(HTTP_STATUS_OK, mockSchema);

    await expect(
      getSinglePolicySchema({
        namespacePath: mockNamespacePath,
        namespaceType: 'project',
        policyType,
      }),
    ).resolves.toStrictEqual(createMockOutput(policyType));
  });

  it('returns an empty schema on request failure', async () => {
    await expect(
      getSinglePolicySchema({
        namespacePath: mockNamespacePath,
        namespaceType: 'project',
        policyType: 'scan_execution_policy',
      }),
    ).resolves.toStrictEqual({});
  });

  it('returns an empty schema on non-existing policy type', async () => {
    await expect(
      getSinglePolicySchema({
        namespacePath: mockNamespacePath,
        namespaceType: 'project',
        policyType: 'non_existent_policy',
      }),
    ).resolves.toStrictEqual({});
  });
});

describe('SecurityPolicySchemaExtension', () => {
  let mock;
  let editor;
  let instance;
  let editorEl;

  const createMockEditor = ({ blobPath = '.gitlab/security-policies/policy.yml' } = {}) => {
    setHTMLFixture('<div id="editor"></div>');
    editorEl = document.getElementById('editor');
    editor = new SourceEditor();
    instance = editor.createInstance({ el: editorEl, blobPath, blobContent: '' });
    instance.use({ definition: SecurityPolicySchemaExtension });
  };

  beforeEach(() => {
    createMockEditor();
    mock = new MockAdapter(axios);
    mock.onGet().reply(HTTP_STATUS_OK, mockSchema);
  });

  afterEach(() => {
    instance.dispose();
    editorEl.remove();
    resetHTMLFixture();
    mock.restore();
  });

  describe('registerSecurityPolicyEditorSchema', () => {
    describe('register validations options with monaco for yaml language', () => {
      it('registers the schema', async () => {
        const policyType = 'scan_execution_policy';
        await instance.registerSecurityPolicyEditorSchema({
          namespacePath: mockNamespacePath,
          namespaceType: 'project',
          policyType,
        });

        expect(registerSchema).toHaveBeenCalledTimes(1);
        expect(registerSchema).toHaveBeenCalledWith({
          uri: `${TEST_HOST}/${mockNamespacePath}/-/security/policies/schema`,
          schema: createMockOutput(policyType),
          fileMatch: ['policy.yml'],
        });
      });
    });
  });

  describe('registerSecurityPolicySchema', () => {
    describe('register validations options with monaco for yaml language', () => {
      it('registers the schema', async () => {
        await instance.registerSecurityPolicySchema(mockNamespacePath);
        expect(registerSchema).toHaveBeenCalledTimes(1);
        expect(registerSchema).toHaveBeenCalledWith({
          uri: `${TEST_HOST}/${mockNamespacePath}/-/security/policies/schema`,
          fileMatch: ['policy.yml'],
        });
      });
    });
  });
});