diff --git a/README.md b/README.md index fb8f2bdb1..19f545cdb 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ The list of props for the AsyncAPI React component includes: - **schema: string | AsyncAPIDocument | object | FetchingSchemaInterface** - The `schema` property is required and contains AsyncAPI specification. Use the `string` type, the [`AsyncAPIDocument`](https://github.com/asyncapi/parser-js/blob/master/lib/models/asyncapi.js) type, parsed specification as JS object from [AsyncAPI Parser](https://github.com/asyncapi/parser-js) or the [`FetchingSchemaInterface`](./library/src/types.ts#L393) object to fetch the schema from an external resource. For more information on what it contains and what it should look like, read [AsyncAPI Specification](https://github.com/asyncapi/asyncapi#asyncapi-specification). + The `schema` property is required and contains AsyncAPI specification. Use the `string` type, the [`AsyncAPIDocument`](https://github.com/asyncapi/parser-js/blob/master/packages/parser/src/models/asyncapi.ts) type, parsed specification as JS object from [AsyncAPI Parser](https://github.com/asyncapi/parser-js) or the [`FetchingSchemaInterface`](./library/src/types.ts#L393) object to fetch the schema from an external resource. For more information on what it contains and what it should look like, read [AsyncAPI Specification](https://github.com/asyncapi/asyncapi#asyncapi-specification). - **config?: Partial** diff --git a/library/src/helpers/__tests__/server.test.ts b/library/src/helpers/__tests__/server.test.ts new file mode 100644 index 000000000..90fa62fa1 --- /dev/null +++ b/library/src/helpers/__tests__/server.test.ts @@ -0,0 +1,112 @@ +import { ServerHelpers } from '../server'; + +describe('ServerHelpers', () => { + describe('.securityType', () => { + test('should map known security types', () => { + expect(ServerHelpers.securityType('apiKey')).toBe('API key'); + expect(ServerHelpers.securityType('oauth2')).toBe('OAuth2'); + expect(ServerHelpers.securityType('scramSha256')).toBe('ScramSha256'); + }); + + test('should fallback to default', () => { + expect(ServerHelpers.securityType('unknown')).toBe('API key'); + }); + }); + + describe('.flowName', () => { + test('should map known flow names', () => { + expect(ServerHelpers.flowName('implicit')).toBe('Implicit'); + expect(ServerHelpers.flowName('password')).toBe('Password'); + expect(ServerHelpers.flowName('authorizationCode')).toBe( + 'Authorization Code', + ); + }); + + test('should fallback to default', () => { + expect(ServerHelpers.flowName('unknown')).toBe('Implicit'); + }); + }); + + describe('.getKafkaSecurity', () => { + const mockSecurity = (type: string) => + ({ + type: () => type, + } as any); + + test('kafka protocol without security', () => { + const result = ServerHelpers.getKafkaSecurity('kafka', null); + expect(result).toEqual({ + securityProtocol: 'PLAINTEXT', + saslMechanism: undefined, + }); + }); + + test('kafka protocol with security', () => { + const result = ServerHelpers.getKafkaSecurity( + 'kafka', + mockSecurity('plain'), + ); + + expect(result).toEqual({ + securityProtocol: 'SASL_PLAINTEXT', + saslMechanism: 'PLAIN', + }); + }); + + test('non-kafka protocol without security', () => { + const result = ServerHelpers.getKafkaSecurity('amqp', null); + expect(result).toEqual({ + securityProtocol: 'SSL', + saslMechanism: undefined, + }); + }); + + test('non-kafka protocol with security', () => { + const result = ServerHelpers.getKafkaSecurity( + 'amqp', + mockSecurity('scramSha256'), + ); + + expect(result).toEqual({ + securityProtocol: 'SASL_SSL', + saslMechanism: 'SCRAM-SHA-256', + }); + }); + + test('scramSha512 mechanism', () => { + const result = ServerHelpers.getKafkaSecurity( + 'kafka', + mockSecurity('scramSha512'), + ); + + expect(result.saslMechanism).toBe('SCRAM-SHA-512'); + }); + + test('oauth2 mechanism', () => { + const result = ServerHelpers.getKafkaSecurity( + 'kafka', + mockSecurity('oauth2'), + ); + + expect(result.saslMechanism).toBe('OAUTHBEARER'); + }); + + test('gssapi mechanism', () => { + const result = ServerHelpers.getKafkaSecurity( + 'kafka', + mockSecurity('gssapi'), + ); + + expect(result.saslMechanism).toBe('GSSAPI'); + }); + + test('X509 forces SSL protocol', () => { + const result = ServerHelpers.getKafkaSecurity( + 'kafka', + mockSecurity('X509'), + ); + + expect(result.securityProtocol).toBe('SSL'); + }); + }); +});