From eb4b72630a9f479f33858fc9011e18666da3bbba Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 6 Apr 2023 09:08:18 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- .../components/oauth_secret_spec.js | 116 +++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 spec/frontend/oauth_application/components/oauth_secret_spec.js (limited to 'spec/frontend/oauth_application') diff --git a/spec/frontend/oauth_application/components/oauth_secret_spec.js b/spec/frontend/oauth_application/components/oauth_secret_spec.js new file mode 100644 index 00000000000..c38bd066da8 --- /dev/null +++ b/spec/frontend/oauth_application/components/oauth_secret_spec.js @@ -0,0 +1,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, + }); + }); + }); + }); + }); +}); -- cgit v1.2.3