mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 08:35:43 -06:00
* ServiceAccounts: able to get upgrade status * Banner with API keys migration info * Show API keys migration info on Service accounts page * Migrate individual API keys * Use transaction for key migration * Migrate all api keys to service accounts * Hide api keys after migration * Migrate API keys separately for each org * Revert API key * Revert key API method * Rename migration actions and reducers * Fix linter errors * Tests for migrating single API key * Tests for migrating all api keys * More tests * Fix reverting tokens * API: rename convert to migrate * Add api route descriptions to methods * rearrange methods in api.go * Refactor: rename and move some methods * Prevent assigning tokens to non-existing service accounts * Refactor: ID TO Id * Refactor: fix error message * Delete service account if migration failed * Fix linter errors
153 lines
4.7 KiB
TypeScript
153 lines
4.7 KiB
TypeScript
import { ApiKeysState } from 'app/types';
|
|
|
|
import { getMultipleMockKeys } from '../__mocks__/apiKeysMock';
|
|
|
|
import { getApiKeys, getApiKeysCount, getIncludeExpired, getIncludeExpiredDisabled } from './selectors';
|
|
|
|
describe('API Keys selectors', () => {
|
|
const mockKeys = getMultipleMockKeys(5);
|
|
const mockKeysIncludingExpired = getMultipleMockKeys(8);
|
|
|
|
describe('getApiKeysCount', () => {
|
|
it('returns the correct count when includeExpired is false', () => {
|
|
const mockState: ApiKeysState = {
|
|
keys: mockKeys,
|
|
keysIncludingExpired: mockKeysIncludingExpired,
|
|
searchQuery: '',
|
|
hasFetched: true,
|
|
includeExpired: false,
|
|
apiKeysMigrated: false,
|
|
};
|
|
const keyCount = getApiKeysCount(mockState);
|
|
expect(keyCount).toBe(5);
|
|
});
|
|
|
|
it('returns the correct count when includeExpired is true', () => {
|
|
const mockState: ApiKeysState = {
|
|
keys: mockKeys,
|
|
keysIncludingExpired: mockKeysIncludingExpired,
|
|
searchQuery: '',
|
|
hasFetched: true,
|
|
includeExpired: true,
|
|
apiKeysMigrated: false,
|
|
};
|
|
const keyCount = getApiKeysCount(mockState);
|
|
expect(keyCount).toBe(8);
|
|
});
|
|
});
|
|
|
|
describe('getApiKeys', () => {
|
|
describe('when includeExpired is false', () => {
|
|
it('should return all keys if no search query', () => {
|
|
const mockState: ApiKeysState = {
|
|
keys: mockKeys,
|
|
keysIncludingExpired: mockKeysIncludingExpired,
|
|
searchQuery: '',
|
|
hasFetched: true,
|
|
includeExpired: false,
|
|
apiKeysMigrated: false,
|
|
};
|
|
const keys = getApiKeys(mockState);
|
|
expect(keys).toEqual(mockKeys);
|
|
});
|
|
|
|
it('should filter keys if search query exists', () => {
|
|
const mockState: ApiKeysState = {
|
|
keys: mockKeys,
|
|
keysIncludingExpired: mockKeysIncludingExpired,
|
|
searchQuery: '5',
|
|
hasFetched: true,
|
|
includeExpired: false,
|
|
apiKeysMigrated: false,
|
|
};
|
|
const keys = getApiKeys(mockState);
|
|
expect(keys.length).toEqual(1);
|
|
});
|
|
});
|
|
|
|
describe('when includeExpired is true', () => {
|
|
it('should return all keys if no search query', () => {
|
|
const mockState: ApiKeysState = {
|
|
keys: mockKeys,
|
|
keysIncludingExpired: mockKeysIncludingExpired,
|
|
searchQuery: '',
|
|
hasFetched: true,
|
|
includeExpired: true,
|
|
apiKeysMigrated: false,
|
|
};
|
|
const keys = getApiKeys(mockState);
|
|
expect(keys).toEqual(mockKeysIncludingExpired);
|
|
});
|
|
|
|
it('should filter keys if search query exists', () => {
|
|
const mockState: ApiKeysState = {
|
|
keys: mockKeys,
|
|
keysIncludingExpired: mockKeysIncludingExpired,
|
|
searchQuery: '5',
|
|
hasFetched: true,
|
|
includeExpired: true,
|
|
apiKeysMigrated: false,
|
|
};
|
|
const keys = getApiKeys(mockState);
|
|
expect(keys.length).toEqual(1);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('getIncludeExpired', () => {
|
|
it('returns true if includeExpired is true', () => {
|
|
const mockState: ApiKeysState = {
|
|
keys: mockKeys,
|
|
keysIncludingExpired: mockKeysIncludingExpired,
|
|
searchQuery: '',
|
|
hasFetched: true,
|
|
includeExpired: true,
|
|
apiKeysMigrated: false,
|
|
};
|
|
const includeExpired = getIncludeExpired(mockState);
|
|
expect(includeExpired).toBe(true);
|
|
});
|
|
|
|
it('returns false if includeExpired is false', () => {
|
|
const mockState: ApiKeysState = {
|
|
keys: mockKeys,
|
|
keysIncludingExpired: mockKeysIncludingExpired,
|
|
searchQuery: '',
|
|
hasFetched: true,
|
|
includeExpired: false,
|
|
apiKeysMigrated: false,
|
|
};
|
|
const includeExpired = getIncludeExpired(mockState);
|
|
expect(includeExpired).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('getIncludeExpiredDisabled', () => {
|
|
it('returns true if there are no active keys but there are expired keys', () => {
|
|
const mockState: ApiKeysState = {
|
|
keys: [],
|
|
keysIncludingExpired: mockKeysIncludingExpired,
|
|
searchQuery: '',
|
|
hasFetched: true,
|
|
includeExpired: true,
|
|
apiKeysMigrated: false,
|
|
};
|
|
const includeExpiredDisabled = getIncludeExpiredDisabled(mockState);
|
|
expect(includeExpiredDisabled).toBe(true);
|
|
});
|
|
|
|
it('returns false otherwise', () => {
|
|
const mockState: ApiKeysState = {
|
|
keys: mockKeys,
|
|
keysIncludingExpired: mockKeysIncludingExpired,
|
|
searchQuery: '',
|
|
hasFetched: true,
|
|
includeExpired: false,
|
|
apiKeysMigrated: false,
|
|
};
|
|
const includeExpiredDisabled = getIncludeExpired(mockState);
|
|
expect(includeExpiredDisabled).toBe(false);
|
|
});
|
|
});
|
|
});
|