forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds UT for auth registry in data source management plugin
Signed-off-by: Bandini Bhopi <bandinib@amazon.com>
- Loading branch information
1 parent
ab6244b
commit 3711d37
Showing
9 changed files
with
136 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...ugins/data_source_management/public/auth_registry/authentication_methods_registry.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { | ||
AuthenticationMethodRegistery, | ||
AuthenticationMethod, | ||
} from './authentication_methods_registry'; | ||
import React from 'react'; | ||
|
||
const createAuthenticationMethod = ( | ||
authMethod: Partial<AuthenticationMethod> | ||
): AuthenticationMethod => ({ | ||
name: 'unknown', | ||
credentialForm: React.createElement('div', {}, 'Hello, world!'), | ||
credentialSourceOption: { | ||
value: 'unknown', | ||
}, | ||
...authMethod, | ||
}); | ||
|
||
describe('AuthenticationMethodRegistery', () => { | ||
let registry: AuthenticationMethodRegistery; | ||
|
||
beforeEach(() => { | ||
registry = new AuthenticationMethodRegistery(); | ||
}); | ||
|
||
it('allows to register authentication method', () => { | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeA' })); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeB' })); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeC' })); | ||
|
||
expect( | ||
registry | ||
.getAllAuthenticationMethods() | ||
.map((type) => type.name) | ||
.sort() | ||
).toEqual(['typeA', 'typeB', 'typeC']); | ||
}); | ||
|
||
it('throws when trying to register the same authentication method twice', () => { | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeA' })); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeB' })); | ||
expect(() => { | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeA' })); | ||
}).toThrowErrorMatchingInlineSnapshot(`"Authentication method 'typeA' is already registered"`); | ||
}); | ||
|
||
describe('#getAuthenticationMethod', () => { | ||
it(`retrieve a type by it's name`, () => { | ||
const typeA = createAuthenticationMethod({ name: 'typeA' }); | ||
const typeB = createAuthenticationMethod({ name: 'typeB' }); | ||
registry.registerAuthenticationMethod(typeA); | ||
registry.registerAuthenticationMethod(typeB); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeC' })); | ||
|
||
expect(registry.getAuthenticationMethod('typeA')).toEqual(typeA); | ||
expect(registry.getAuthenticationMethod('typeB')).toEqual(typeB); | ||
expect(registry.getAuthenticationMethod('unknownType')).toBeUndefined(); | ||
}); | ||
|
||
it('forbids to mutate the registered types', () => { | ||
registry.registerAuthenticationMethod( | ||
createAuthenticationMethod({ | ||
name: 'typeA', | ||
}) | ||
); | ||
|
||
const typeA = registry.getAuthenticationMethod('typeA')!; | ||
|
||
expect(() => { | ||
typeA.credentialForm = React.createElement('div', {}, 'Welcome!'); | ||
}).toThrow(); | ||
expect(() => { | ||
typeA.credentialSourceOption = { | ||
value: 'typeA', | ||
}; | ||
}).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('#getAllTypes', () => { | ||
it('returns all registered types', () => { | ||
const typeA = createAuthenticationMethod({ name: 'typeA' }); | ||
const typeB = createAuthenticationMethod({ name: 'typeB' }); | ||
const typeC = createAuthenticationMethod({ name: 'typeC' }); | ||
registry.registerAuthenticationMethod(typeA); | ||
registry.registerAuthenticationMethod(typeB); | ||
|
||
const registered = registry.getAllAuthenticationMethods(); | ||
expect(registered.length).toEqual(2); | ||
expect(registered).toContainEqual(typeA); | ||
expect(registered).toContainEqual(typeB); | ||
expect(registered).not.toContainEqual(typeC); | ||
}); | ||
|
||
it('does not mutate the registered types when altering the list', () => { | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeA' })); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeB' })); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeC' })); | ||
|
||
const types = registry.getAllAuthenticationMethods(); | ||
types.splice(0, 3); | ||
|
||
expect(registry.getAllAuthenticationMethods().length).toEqual(3); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters