|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { nextTick } from '@kbn/test/jest'; |
| 9 | +import { coreMock } from 'src/core/public/mocks'; |
| 10 | +import { homePluginMock } from 'src/plugins/home/public/mocks'; |
| 11 | +import { securityMock } from '../../security/public/mocks'; |
| 12 | +import { CloudPlugin } from './plugin'; |
| 13 | + |
| 14 | +describe('Cloud Plugin', () => { |
| 15 | + describe('#start', () => { |
| 16 | + function setupPlugin({ |
| 17 | + roles = [], |
| 18 | + simulateUserError = false, |
| 19 | + }: { roles?: string[]; simulateUserError?: boolean } = {}) { |
| 20 | + const plugin = new CloudPlugin( |
| 21 | + coreMock.createPluginInitializerContext({ |
| 22 | + id: 'cloudId', |
| 23 | + base_url: 'https://cloud.elastic.co', |
| 24 | + deployment_url: '/abc123', |
| 25 | + profile_url: '/profile/alice', |
| 26 | + organization_url: '/org/myOrg', |
| 27 | + }) |
| 28 | + ); |
| 29 | + const coreSetup = coreMock.createSetup(); |
| 30 | + const homeSetup = homePluginMock.createSetupContract(); |
| 31 | + const securitySetup = securityMock.createSetup(); |
| 32 | + if (simulateUserError) { |
| 33 | + securitySetup.authc.getCurrentUser.mockRejectedValue(new Error('Something happened')); |
| 34 | + } else { |
| 35 | + securitySetup.authc.getCurrentUser.mockResolvedValue( |
| 36 | + securityMock.createMockAuthenticatedUser({ |
| 37 | + roles, |
| 38 | + }) |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + plugin.setup(coreSetup, { home: homeSetup, security: securitySetup }); |
| 43 | + |
| 44 | + return { coreSetup, securitySetup, plugin }; |
| 45 | + } |
| 46 | + |
| 47 | + it('registers help support URL', async () => { |
| 48 | + const { plugin } = setupPlugin(); |
| 49 | + |
| 50 | + const coreStart = coreMock.createStart(); |
| 51 | + const securityStart = securityMock.createStart(); |
| 52 | + plugin.start(coreStart, { security: securityStart }); |
| 53 | + |
| 54 | + expect(coreStart.chrome.setHelpSupportUrl).toHaveBeenCalledTimes(1); |
| 55 | + expect(coreStart.chrome.setHelpSupportUrl.mock.calls[0]).toMatchInlineSnapshot(` |
| 56 | + Array [ |
| 57 | + "https://support.elastic.co/", |
| 58 | + ] |
| 59 | + `); |
| 60 | + }); |
| 61 | + |
| 62 | + it('registers a custom nav link for superusers', async () => { |
| 63 | + const { plugin } = setupPlugin({ roles: ['superuser'] }); |
| 64 | + |
| 65 | + const coreStart = coreMock.createStart(); |
| 66 | + const securityStart = securityMock.createStart(); |
| 67 | + plugin.start(coreStart, { security: securityStart }); |
| 68 | + |
| 69 | + await nextTick(); |
| 70 | + |
| 71 | + expect(coreStart.chrome.setCustomNavLink).toHaveBeenCalledTimes(1); |
| 72 | + expect(coreStart.chrome.setCustomNavLink.mock.calls[0]).toMatchInlineSnapshot(` |
| 73 | + Array [ |
| 74 | + Object { |
| 75 | + "euiIconType": "arrowLeft", |
| 76 | + "href": "https://cloud.elastic.co/abc123", |
| 77 | + "title": "Manage this deployment", |
| 78 | + }, |
| 79 | + ] |
| 80 | + `); |
| 81 | + }); |
| 82 | + |
| 83 | + it('registers a custom nav link when there is an error retrieving the current user', async () => { |
| 84 | + const { plugin } = setupPlugin({ simulateUserError: true }); |
| 85 | + |
| 86 | + const coreStart = coreMock.createStart(); |
| 87 | + const securityStart = securityMock.createStart(); |
| 88 | + plugin.start(coreStart, { security: securityStart }); |
| 89 | + |
| 90 | + await nextTick(); |
| 91 | + |
| 92 | + expect(coreStart.chrome.setCustomNavLink).toHaveBeenCalledTimes(1); |
| 93 | + expect(coreStart.chrome.setCustomNavLink.mock.calls[0]).toMatchInlineSnapshot(` |
| 94 | + Array [ |
| 95 | + Object { |
| 96 | + "euiIconType": "arrowLeft", |
| 97 | + "href": "https://cloud.elastic.co/abc123", |
| 98 | + "title": "Manage this deployment", |
| 99 | + }, |
| 100 | + ] |
| 101 | + `); |
| 102 | + }); |
| 103 | + |
| 104 | + it('does not register a custom nav link for non-superusers', async () => { |
| 105 | + const { plugin } = setupPlugin({ roles: ['not-a-superuser'] }); |
| 106 | + |
| 107 | + const coreStart = coreMock.createStart(); |
| 108 | + const securityStart = securityMock.createStart(); |
| 109 | + plugin.start(coreStart, { security: securityStart }); |
| 110 | + |
| 111 | + await nextTick(); |
| 112 | + |
| 113 | + expect(coreStart.chrome.setCustomNavLink).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('registers user profile links for superusers', async () => { |
| 117 | + const { plugin } = setupPlugin({ roles: ['superuser'] }); |
| 118 | + |
| 119 | + const coreStart = coreMock.createStart(); |
| 120 | + const securityStart = securityMock.createStart(); |
| 121 | + plugin.start(coreStart, { security: securityStart }); |
| 122 | + |
| 123 | + await nextTick(); |
| 124 | + |
| 125 | + expect(securityStart.navControlService.addUserMenuLinks).toHaveBeenCalledTimes(1); |
| 126 | + expect(securityStart.navControlService.addUserMenuLinks.mock.calls[0]).toMatchInlineSnapshot(` |
| 127 | + Array [ |
| 128 | + Array [ |
| 129 | + Object { |
| 130 | + "href": "https://cloud.elastic.co/profile/alice", |
| 131 | + "iconType": "user", |
| 132 | + "label": "Profile", |
| 133 | + "order": 100, |
| 134 | + "setAsProfile": true, |
| 135 | + }, |
| 136 | + Object { |
| 137 | + "href": "https://cloud.elastic.co/org/myOrg", |
| 138 | + "iconType": "gear", |
| 139 | + "label": "Account & Billing", |
| 140 | + "order": 200, |
| 141 | + }, |
| 142 | + ], |
| 143 | + ] |
| 144 | + `); |
| 145 | + }); |
| 146 | + |
| 147 | + it('registers profile links when there is an error retrieving the current user', async () => { |
| 148 | + const { plugin } = setupPlugin({ simulateUserError: true }); |
| 149 | + |
| 150 | + const coreStart = coreMock.createStart(); |
| 151 | + const securityStart = securityMock.createStart(); |
| 152 | + plugin.start(coreStart, { security: securityStart }); |
| 153 | + |
| 154 | + await nextTick(); |
| 155 | + |
| 156 | + expect(securityStart.navControlService.addUserMenuLinks).toHaveBeenCalledTimes(1); |
| 157 | + expect(securityStart.navControlService.addUserMenuLinks.mock.calls[0]).toMatchInlineSnapshot(` |
| 158 | + Array [ |
| 159 | + Array [ |
| 160 | + Object { |
| 161 | + "href": "https://cloud.elastic.co/profile/alice", |
| 162 | + "iconType": "user", |
| 163 | + "label": "Profile", |
| 164 | + "order": 100, |
| 165 | + "setAsProfile": true, |
| 166 | + }, |
| 167 | + Object { |
| 168 | + "href": "https://cloud.elastic.co/org/myOrg", |
| 169 | + "iconType": "gear", |
| 170 | + "label": "Account & Billing", |
| 171 | + "order": 200, |
| 172 | + }, |
| 173 | + ], |
| 174 | + ] |
| 175 | + `); |
| 176 | + }); |
| 177 | + |
| 178 | + it('does not register profile links for non-superusers', async () => { |
| 179 | + const { plugin } = setupPlugin({ roles: ['not-a-superuser'] }); |
| 180 | + |
| 181 | + const coreStart = coreMock.createStart(); |
| 182 | + const securityStart = securityMock.createStart(); |
| 183 | + plugin.start(coreStart, { security: securityStart }); |
| 184 | + |
| 185 | + await nextTick(); |
| 186 | + |
| 187 | + expect(securityStart.navControlService.addUserMenuLinks).not.toHaveBeenCalled(); |
| 188 | + }); |
| 189 | + }); |
| 190 | +}); |
0 commit comments