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

revoke_token_button_spec.js « components « agents « clusters « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 970782a8e582f8f78b58938e3a47650b9a8f2dd6 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { GlButton, GlModal, GlFormInput, GlTooltip } from '@gitlab/ui';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { ENTER_KEY } from '~/lib/utils/keys';
import RevokeTokenButton from '~/clusters/agents/components/revoke_token_button.vue';
import getClusterAgentQuery from '~/clusters/agents/graphql/queries/get_cluster_agent.query.graphql';
import revokeTokenMutation from '~/clusters/agents/graphql/mutations/revoke_token.mutation.graphql';
import { MAX_LIST_COUNT } from '~/clusters/agents/constants';
import { getTokenResponse, mockRevokeResponse, mockErrorRevokeResponse } from '../../mock_data';

Vue.use(VueApollo);

describe('RevokeTokenButton', () => {
  let wrapper;
  let toast;
  let apolloProvider;
  let revokeSpy;

  const token = {
    id: 'token-id',
    name: 'token-name',
  };
  const cursor = {
    first: MAX_LIST_COUNT,
    last: null,
  };
  const agentName = 'cluster-agent';
  const projectPath = 'path/to/project';

  const defaultProvide = {
    agentName,
    projectPath,
    canAdminCluster: true,
  };
  const propsData = {
    token,
    cursor,
  };

  const findModal = () => wrapper.findComponent(GlModal);
  const findRevokeBtn = () => wrapper.findComponent(GlButton);
  const findInput = () => wrapper.findComponent(GlFormInput);
  const findTooltip = () => wrapper.findComponent(GlTooltip);
  const findPrimaryAction = () => findModal().props('actionPrimary');
  const findPrimaryActionAttributes = (attr) => findPrimaryAction().attributes[attr];

  const createMockApolloProvider = ({ mutationResponse }) => {
    revokeSpy = jest.fn().mockResolvedValue(mutationResponse);

    return createMockApollo([[revokeTokenMutation, revokeSpy]]);
  };

  const writeQuery = () => {
    apolloProvider.clients.defaultClient.cache.writeQuery({
      query: getClusterAgentQuery,
      variables: {
        agentName,
        projectPath,
        ...cursor,
      },
      data: getTokenResponse.data,
    });
  };

  const createWrapper = async ({
    mutationResponse = mockRevokeResponse,
    provideData = {},
  } = {}) => {
    apolloProvider = createMockApolloProvider({ mutationResponse });

    toast = jest.fn();

    wrapper = shallowMountExtended(RevokeTokenButton, {
      apolloProvider,
      provide: {
        ...defaultProvide,
        ...provideData,
      },
      propsData,
      stubs: {
        GlModal,
        GlTooltip,
      },
      mocks: { $toast: { show: toast } },
    });
    wrapper.vm.$refs.modal.hide = jest.fn();

    writeQuery();
    await nextTick();
  };

  const submitTokenToRevoke = async () => {
    findRevokeBtn().vm.$emit('click');
    findInput().vm.$emit('input', token.name);
    await findModal().vm.$emit('primary');
    await waitForPromises();
  };

  beforeEach(() => {
    createWrapper();
  });

  afterEach(() => {
    apolloProvider = null;
    revokeSpy = null;
  });

  describe('revoke token action', () => {
    it('displays a revoke button', () => {
      expect(findRevokeBtn().attributes('aria-label')).toBe('Revoke token');
    });

    describe('when user cannot revoke token', () => {
      beforeEach(() => {
        createWrapper({ provideData: { canAdminCluster: false } });
      });

      it('disabled the button', () => {
        expect(findRevokeBtn().attributes('disabled')).toBeDefined();
      });

      it('shows a disabled tooltip', () => {
        expect(findTooltip().attributes('title')).toBe(
          'Requires a Maintainer or greater role to perform this action',
        );
      });
    });

    describe('when user can create a token and clicks the button', () => {
      beforeEach(() => {
        findRevokeBtn().vm.$emit('click');
      });

      it('displays a delete confirmation modal', () => {
        expect(findModal().isVisible()).toBe(true);
      });

      describe.each`
        condition                                   | tokenName       | isDisabled | mutationCalled
        ${'the input with token name is missing'}   | ${''}           | ${true}    | ${false}
        ${'the input with token name is incorrect'} | ${'wrong-name'} | ${true}    | ${false}
        ${'the input with token name is correct'}   | ${token.name}   | ${false}   | ${true}
      `('when $condition', ({ tokenName, isDisabled, mutationCalled }) => {
        beforeEach(() => {
          findRevokeBtn().vm.$emit('click');
          findInput().vm.$emit('input', tokenName);
        });

        it(`${isDisabled ? 'disables' : 'enables'} the modal primary button`, () => {
          expect(findPrimaryActionAttributes('disabled')).toBe(isDisabled);
        });

        describe('when user clicks the modal primary button', () => {
          beforeEach(async () => {
            await findModal().vm.$emit('primary');
          });

          if (mutationCalled) {
            it('calls the revoke mutation', () => {
              expect(revokeSpy).toHaveBeenCalledWith({ input: { id: token.id } });
            });
          } else {
            it("doesn't call the revoke mutation", () => {
              expect(revokeSpy).not.toHaveBeenCalled();
            });
          }
        });

        describe('when user presses the enter button', () => {
          beforeEach(async () => {
            await findInput().vm.$emit('keydown', new KeyboardEvent({ key: ENTER_KEY }));
          });

          if (mutationCalled) {
            it('calls the revoke mutation', () => {
              expect(revokeSpy).toHaveBeenCalledWith({ input: { id: token.id } });
            });
          } else {
            it("doesn't call the revoke mutation", () => {
              expect(revokeSpy).not.toHaveBeenCalled();
            });
          }
        });
      });
    });

    describe('when token was revoked successfully', () => {
      beforeEach(async () => {
        await submitTokenToRevoke();
      });

      it('calls the toast action', () => {
        expect(toast).toHaveBeenCalledWith(`${token.name} successfully revoked`);
      });
    });

    describe('when getting an error revoking token', () => {
      beforeEach(async () => {
        await createWrapper({ mutationResponse: mockErrorRevokeResponse });
        await submitTokenToRevoke();
      });

      it('displays the error message', () => {
        expect(toast).toHaveBeenCalledWith('could not revoke token');
      });
    });

    describe('when the revoke modal was closed', () => {
      beforeEach(async () => {
        const loadingResponse = new Promise(() => {});
        await createWrapper({ mutationResponse: loadingResponse });
        await submitTokenToRevoke();
      });

      it('reenables the button', async () => {
        expect(findPrimaryActionAttributes('loading')).toBe(true);
        expect(findRevokeBtn().attributes('disabled')).toBeDefined();

        await findModal().vm.$emit('hide');

        expect(findPrimaryActionAttributes('loading')).toBe(false);
        expect(findRevokeBtn().attributes('disabled')).toBeUndefined();
      });

      it('clears the token name input', async () => {
        expect(findInput().attributes('value')).toBe(token.name);

        await findModal().vm.$emit('hide');

        expect(findInput().attributes('value')).toBeUndefined();
      });
    });
  });
});