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

oauth_secret_spec.js « components « oauth_application « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c38bd066da82be8d070dbab0fcf9a171bf706286 (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
import { GlButton, GlModal } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import waitForPromises from 'helpers/wait_for_promises';
import { createAlert, VARIANT_SUCCESS, VARIANT_WARNING } from '~/alert';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
import OAuthSecret from '~/oauth_application/components/oauth_secret.vue';
import {
  RENEW_SECRET_FAILURE,
  RENEW_SECRET_SUCCESS,
  WARNING_NO_SECRET,
} from '~/oauth_application/constants';
import InputCopyToggleVisibility from '~/vue_shared/components/form/input_copy_toggle_visibility.vue';

jest.mock('~/alert');
const mockEvent = { preventDefault: jest.fn() };

describe('OAuthSecret', () => {
  let wrapper;
  const renewPath = '/applications/1/renew';

  const createComponent = (provide = {}) => {
    wrapper = shallowMount(OAuthSecret, {
      provide: {
        initialSecret: undefined,
        renewPath,
        ...provide,
      },
    });
  };

  const findInputCopyToggleVisibility = () => wrapper.findComponent(InputCopyToggleVisibility);
  const findRenewSecretButton = () => wrapper.findComponent(GlButton);
  const findModal = () => wrapper.findComponent(GlModal);

  describe('when secret is provided', () => {
    const initialSecret = 'my secret';
    beforeEach(() => {
      createComponent({ initialSecret });
    });

    it('shows the masked secret', () => {
      expect(findInputCopyToggleVisibility().props('value')).toBe(initialSecret);
    });

    it('shows the renew secret button', () => {
      expect(findRenewSecretButton().exists()).toBe(true);
    });
  });

  describe('when secret is not provided', () => {
    beforeEach(() => {
      createComponent();
    });

    it('shows an alert', () => {
      expect(createAlert).toHaveBeenCalledWith({
        message: WARNING_NO_SECRET,
        variant: VARIANT_WARNING,
      });
    });

    it('shows the renew secret button', () => {
      expect(findRenewSecretButton().exists()).toBe(true);
    });

    describe('when renew secret button is selected', () => {
      beforeEach(() => {
        createComponent();
        findRenewSecretButton().vm.$emit('click');
      });

      it('shows a modal', () => {
        expect(findModal().props('visible')).toBe(true);
      });

      describe('when secret renewal succeeds', () => {
        const initialSecret = 'my secret';

        beforeEach(async () => {
          const mockAxios = new MockAdapter(axios);
          mockAxios.onPut().reply(HTTP_STATUS_OK, { secret: initialSecret });
          findModal().vm.$emit('primary', mockEvent);
          await waitForPromises();
        });

        it('shows an alert', () => {
          expect(createAlert).toHaveBeenCalledWith({
            message: RENEW_SECRET_SUCCESS,
            variant: VARIANT_SUCCESS,
          });
        });

        it('shows the new secret', () => {
          expect(findInputCopyToggleVisibility().props('value')).toBe(initialSecret);
        });
      });

      describe('when secret renewal fails', () => {
        beforeEach(async () => {
          const mockAxios = new MockAdapter(axios);
          mockAxios.onPut().reply(HTTP_STATUS_INTERNAL_SERVER_ERROR);
          findModal().vm.$emit('primary', mockEvent);
          await waitForPromises();
        });

        it('creates an alert', () => {
          expect(createAlert).toHaveBeenCalledWith({
            message: RENEW_SECRET_FAILURE,
          });
        });
      });
    });
  });
});