|
| 1 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { AuthContext } from './auth'; |
| 3 | +import { TempoError } from '@tempojs/common'; |
| 4 | + |
| 5 | +describe('AuthContext', () => { |
| 6 | + let authContext: AuthContext; |
| 7 | + let key: string; |
| 8 | + let propertyName: string; |
| 9 | + let propertyValue: unknown; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + authContext = new AuthContext(); |
| 13 | + key = 'key1'; |
| 14 | + propertyName = 'prop1'; |
| 15 | + propertyValue = 'value1'; |
| 16 | + authContext.addProperty(key, propertyName, propertyValue); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should add a new property', () => { |
| 20 | + const prop = authContext.findPropertyByName(key, propertyName); |
| 21 | + expect(prop).toBeDefined(); |
| 22 | + expect(prop?.name).toEqual(propertyName); |
| 23 | + expect(prop?.getValue()).toEqual(propertyValue); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should return peer identity if authenticated', () => { |
| 27 | + authContext.peerIdentityKey = key; |
| 28 | + const peerIdentity = authContext.peerIdentity; |
| 29 | + expect(peerIdentity).toBeDefined(); |
| 30 | + expect(peerIdentity?.[0]?.name).toEqual(propertyName); |
| 31 | + expect(peerIdentity?.[0]?.getValue()).toEqual(propertyValue); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should throw TempoError when setting undefined key as peerIdentityKey', () => { |
| 35 | + expect(() => { |
| 36 | + authContext.peerIdentityKey = undefined; |
| 37 | + }).toThrow(TempoError); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should throw TempoError when setting a non-existent key as peerIdentityKey', () => { |
| 41 | + expect(() => { |
| 42 | + authContext.peerIdentityKey = 'nonExistentKey'; |
| 43 | + }).toThrow(TempoError); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return undefined for non-authenticated peer', () => { |
| 47 | + const nonAuthenticatedContext = new AuthContext(); |
| 48 | + const peerIdentity = nonAuthenticatedContext.peerIdentity; |
| 49 | + expect(peerIdentity).toBeUndefined(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return undefined for non-existing property', () => { |
| 53 | + const prop = authContext.findPropertyByName(key, 'nonExistingProperty'); |
| 54 | + expect(prop).toBeUndefined(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return undefined for properties of non-existing key', () => { |
| 58 | + const properties = authContext.getProperties('nonExistingKey'); |
| 59 | + expect(properties).toBeUndefined(); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('AuthContext - Edge Cases', () => { |
| 64 | + let authContext: AuthContext; |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + authContext = new AuthContext(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle adding a property with null or undefined key', () => { |
| 71 | + expect(() => { |
| 72 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 73 | + //@ts-ignore |
| 74 | + authContext.addProperty(null, 'propertyName', 'value'); |
| 75 | + }).toThrow(TempoError); |
| 76 | + expect(() => { |
| 77 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 78 | + //@ts-ignore |
| 79 | + authContext.addProperty(undefined, 'propertyName', 'value'); |
| 80 | + }).toThrow(TempoError); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should handle adding a property with null or undefined name', () => { |
| 84 | + expect(() => { |
| 85 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 86 | + //@ts-ignore |
| 87 | + authContext.addProperty('key', null, 'value'); |
| 88 | + }).toThrow(TempoError); |
| 89 | + expect(() => { |
| 90 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 91 | + //@ts-ignore |
| 92 | + authContext.addProperty('key', undefined, 'value'); |
| 93 | + }).toThrow(TempoError); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should handle adding a property with undefined value', () => { |
| 97 | + expect(() => authContext.addProperty('key', 'propertyName', undefined)).toThrow(TempoError); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should handle multiple properties with the same key and name', () => { |
| 101 | + const value1 = 'value1'; |
| 102 | + const value2 = 'value2'; |
| 103 | + authContext.addProperty('key', 'propertyName', value1); |
| 104 | + authContext.addProperty('key', 'propertyName', value2); |
| 105 | + const properties = authContext.findPropertiesByName('key', 'propertyName'); |
| 106 | + expect(properties).toBeDefined(); |
| 107 | + expect(properties?.length).toEqual(2); |
| 108 | + expect(properties?.[0]?.getValue()).toEqual(value1); |
| 109 | + expect(properties?.[1]?.getValue()).toEqual(value2); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should throw error when trying to set peer identity with non-existing key', () => { |
| 113 | + expect(() => { |
| 114 | + authContext.peerIdentityKey = 'nonExistentKey'; |
| 115 | + }).toThrow(TempoError); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should handle setting peer identity with a key that exists but has no properties', () => { |
| 119 | + const emptyKey = 'emptyKey'; |
| 120 | + authContext.addProperty(emptyKey, 'propertyName', 'value'); |
| 121 | + authContext.getProperties(emptyKey)?.pop(); |
| 122 | + expect(() => { |
| 123 | + authContext.peerIdentityKey = emptyKey; |
| 124 | + }).not.toThrow(); |
| 125 | + expect(authContext.peerIdentity).length(0); |
| 126 | + }); |
| 127 | +}); |
0 commit comments