LicensePage.test.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { LicenseInfo, Props } from './LicenseInfo';
  4. import { LicenseToken } from './types';
  5. import * as index from './index';
  6. jest.mock('@grafana/runtime/src/config', () => ({
  7. config: {
  8. buildInfo: {},
  9. bootData: { navTree: [] },
  10. licensing: {
  11. limitBy: 'users',
  12. includedUsers: -1,
  13. slug: '',
  14. licenseExpiry: 1610976490,
  15. licenseExpiryWarnDays: 0,
  16. tokenExpiry: 1610976490,
  17. tokenExpiryWarnDays: 0,
  18. usageBilling: false,
  19. },
  20. licenseInfo: {},
  21. featureToggles: {
  22. accesscontrol: false,
  23. },
  24. },
  25. }));
  26. // set Grafana Admin to true to give the right permissions to view the licensing page and warnings
  27. jest.mock('app/core/services/context_srv', () => {
  28. const contextSrv = jest.requireActual('../../core/services/context_srv');
  29. contextSrv.contextSrv.isGrafanaAdmin = true;
  30. return contextSrv;
  31. });
  32. jest.spyOn(index, 'initLicenseWarnings').mockImplementation(() => {});
  33. const validToken: LicenseToken = {
  34. status: 1,
  35. jti: '805',
  36. iss: 'https://test.com',
  37. sub: 'https://test.com',
  38. iat: 1578576782,
  39. exp: 2610976490,
  40. nbf: 1578576526,
  41. lexp: 1610976490,
  42. lid: '10500',
  43. limit_by: 'users',
  44. included_users: 110,
  45. prod: ['grafana-enterprise'],
  46. company: 'Test',
  47. slug: '',
  48. };
  49. const validUsersToken: LicenseToken = {
  50. status: 1,
  51. jti: '805',
  52. iss: 'https://test.com',
  53. sub: 'https://test.com',
  54. iat: 1578576782,
  55. exp: 2610976490,
  56. nbf: 1578576526,
  57. lexp: 1610976490,
  58. lid: '10500',
  59. limit_by: 'users',
  60. included_users: 110,
  61. prod: ['grafana-enterprise'],
  62. company: 'Test',
  63. slug: '',
  64. };
  65. const expiredToken: LicenseToken = {
  66. status: 5,
  67. jti: '14',
  68. iss: 'https://test.com',
  69. sub: 'https://test.com',
  70. iat: 1539191907,
  71. exp: 1577854800,
  72. nbf: 1539191759,
  73. lexp: 1577854800,
  74. lid: '5',
  75. limit_by: 'users',
  76. included_users: -1,
  77. prod: ['grafana-enterprise'],
  78. company: 'Test',
  79. slug: '',
  80. };
  81. const trialToken: LicenseToken = {
  82. status: 1,
  83. jti: '805',
  84. iss: 'https://test.com',
  85. sub: 'https://test.com',
  86. iat: 1578576782,
  87. exp: 2610976490,
  88. nbf: 1578576526,
  89. lexp: 1610976490,
  90. lid: '10500',
  91. limit_by: 'users',
  92. included_users: 110,
  93. prod: ['grafana-enterprise'],
  94. company: 'Test',
  95. slug: '',
  96. trial: true,
  97. trial_exp: 2610976490,
  98. };
  99. const trialExpiredToken: LicenseToken = {
  100. status: 1,
  101. jti: '805',
  102. iss: 'https://test.com',
  103. sub: 'https://test.com',
  104. iat: 1578576782,
  105. exp: 2610976490,
  106. nbf: 1578576526,
  107. lexp: 1610976490,
  108. lid: '10500',
  109. limit_by: 'users',
  110. included_users: 110,
  111. prod: ['grafana-enterprise'],
  112. company: 'Test',
  113. slug: '',
  114. trial: true,
  115. trial_exp: 1577854800,
  116. };
  117. const activeUserStats = {
  118. active_users: 3,
  119. };
  120. afterEach(jest.clearAllMocks);
  121. const setup = (propOverrides?: Partial<Props>) => {
  122. const props: Props = {
  123. tokenUpdated: false,
  124. tokenRenewed: false,
  125. token: validToken,
  126. stats: activeUserStats,
  127. };
  128. Object.assign(props, propOverrides);
  129. render(<LicenseInfo {...props} />);
  130. };
  131. describe('LicensePage', () => {
  132. it('should show license info for valid license', async () => {
  133. setup();
  134. expect(await screen.findByRole('heading', { name: 'License' })).toBeInTheDocument();
  135. expect(screen.getByRole('heading', { name: 'Token' })).toBeInTheDocument();
  136. expect(screen.getByRole('heading', { name: 'Utilization' })).toBeInTheDocument();
  137. expect(screen.queryByRole('alert')).not.toBeInTheDocument();
  138. });
  139. it('should show license warning for invalid license', async () => {
  140. setup({ token: expiredToken });
  141. expect(await screen.findAllByRole('alert')).toHaveLength(3);
  142. expect(
  143. screen.getAllByText('Contact support to renew your token, or visit the Cloud portal to learn more.')
  144. ).toHaveLength(2);
  145. expect(screen.getByText('Token expired')).toBeInTheDocument();
  146. });
  147. it('should show warning when user utilization is reaching limits', async () => {
  148. setup({ token: validUsersToken, stats: { ...activeUserStats, active_users: 101 } });
  149. expect(await screen.findAllByRole('alert')).toHaveLength(2);
  150. expect(screen.getByText('Reaching limit: active users')).toBeInTheDocument();
  151. expect(screen.getByText('User utilization reaching limit')).toBeInTheDocument();
  152. expect(screen.getByText(/There are 101 active users. You are approaching your limit of 110 active users/i));
  153. });
  154. it('should show warning when user utilization is at limits', async () => {
  155. setup({ token: validUsersToken, stats: { ...activeUserStats, active_users: 110 } });
  156. expect(await screen.findAllByRole('alert')).toHaveLength(2);
  157. expect(screen.getByText('Limit reached: active users')).toBeInTheDocument();
  158. expect(screen.getByText('User utilization limit reached')).toBeInTheDocument();
  159. expect(screen.getByText(/You are at your limit of 110 active users/i));
  160. });
  161. it('should show alert when user utilization is over limits', async () => {
  162. setup({ token: validUsersToken, stats: { ...activeUserStats, active_users: 111 } });
  163. expect(await screen.findAllByRole('alert')).toHaveLength(2);
  164. expect(screen.getByText('Quota exceeded: active users')).toBeInTheDocument();
  165. expect(screen.getByText('User limit exceeded')).toBeInTheDocument();
  166. expect(screen.getByText(/There are more than 110 active users using Grafana./i));
  167. });
  168. it('should show notice when in a trial', async () => {
  169. setup({ token: trialToken });
  170. expect(await screen.findAllByRole('alert')).toHaveLength(1);
  171. expect(screen.getByText('Trial active')).toBeInTheDocument();
  172. expect(screen.getByText(/Your trial expires in \d+ day\(s\)/i)).toBeInTheDocument();
  173. });
  174. it('should show notice when a trial has expired', async () => {
  175. setup({ token: trialExpiredToken });
  176. expect(await screen.findAllByRole('alert')).toHaveLength(1);
  177. expect(screen.getByText('Trial expired')).toBeInTheDocument();
  178. expect(screen.getByText(/Your trial period has expired/i)).toBeInTheDocument();
  179. });
  180. it('should construct a details link from token properties if none is provided', async () => {
  181. setup({ token: validToken });
  182. expect(await screen.findByRole('heading', { name: 'License' })).toBeInTheDocument();
  183. const licenseDetailsButton = screen.getByLabelText(/View details about your license/);
  184. expect(licenseDetailsButton).toBeInTheDocument();
  185. expect(licenseDetailsButton).toHaveAttribute('href', `${validToken.iss}/licenses/${validToken.lid}`);
  186. });
  187. it('should use details link provided in token', async () => {
  188. setup({ token: { ...validToken, details_url: 'http://example.com/foobar-license' } });
  189. expect(await screen.findByRole('heading', { name: 'License' })).toBeInTheDocument();
  190. const licenseDetailsButton = screen.getByLabelText(/View details about your license/);
  191. expect(licenseDetailsButton).toBeInTheDocument();
  192. expect(licenseDetailsButton).toHaveAttribute('href', 'http://example.com/foobar-license');
  193. });
  194. });