|
1 | 1 | // Basic test to confirm destination structure |
2 | 2 | import Destination from '../index' |
3 | | -import type { DestinationDefinition } from '@segment/actions-core' |
| 3 | +import type { DestinationDefinition, RequestClient } from '@segment/actions-core' |
| 4 | +import { Region } from '../types' |
4 | 5 |
|
5 | 6 | describe('Amazon Conversions API Destination', () => { |
6 | 7 | it('should be properly configured', () => { |
@@ -113,4 +114,96 @@ describe('Amazon Conversions API Destination', () => { |
113 | 114 | } |
114 | 115 | }) |
115 | 116 | }) |
| 117 | + |
| 118 | + describe('testAuthentication', () => { |
| 119 | + const testAuth = Destination.authentication.testAuthentication! |
| 120 | + let mockRequest: jest.Mock |
| 121 | + |
| 122 | + beforeEach(() => { |
| 123 | + mockRequest = jest.fn().mockResolvedValue({ status: 200 }) |
| 124 | + }) |
| 125 | + |
| 126 | + it('should throw for non-numeric advertiserId', async () => { |
| 127 | + await expect( |
| 128 | + testAuth(mockRequest as unknown as RequestClient, { |
| 129 | + auth: { accessToken: 'valid-token', refreshToken: '' }, |
| 130 | + settings: { region: Region.NA, advertiserId: 'abc123' } |
| 131 | + } as any) |
| 132 | + ).rejects.toThrow('Advertising ID must be numeric') |
| 133 | + }) |
| 134 | + |
| 135 | + it('should throw for advertiserId not numeric', async () => { |
| 136 | + await expect( |
| 137 | + testAuth(mockRequest as unknown as RequestClient, { |
| 138 | + auth: { accessToken: 'valid-token', refreshToken: '' }, |
| 139 | + settings: { region: Region.NA, advertiserId: '1234ac' } |
| 140 | + } as any) |
| 141 | + ).rejects.toThrow('Advertising ID must be numeric') |
| 142 | + }) |
| 143 | + |
| 144 | + it('should throw for dataSetName that is too short (under 5 chars)', async () => { |
| 145 | + await expect( |
| 146 | + testAuth(mockRequest as unknown as RequestClient, { |
| 147 | + auth: { accessToken: 'valid-token', refreshToken: '' }, |
| 148 | + settings: { region: Region.NA, advertiserId: '12345', dataSetName: 'abcd' } |
| 149 | + } as any) |
| 150 | + ).rejects.toThrow('Dataset Name must start with a letter and can only contain letters, numbers, underscores, or hyphens.') |
| 151 | + }) |
| 152 | + |
| 153 | + it('should throw for dataSetName that starts with a digit', async () => { |
| 154 | + await expect( |
| 155 | + testAuth(mockRequest as unknown as RequestClient, { |
| 156 | + auth: { accessToken: 'valid-token', refreshToken: '' }, |
| 157 | + settings: { region: Region.NA, advertiserId: '12345', dataSetName: '1invalid' } |
| 158 | + } as any) |
| 159 | + ).rejects.toThrow('Dataset Name must start with a letter and can only contain letters, numbers, underscores, or hyphens.') |
| 160 | + }) |
| 161 | + |
| 162 | + it('should throw for dataSetName with invalid characters', async () => { |
| 163 | + await expect( |
| 164 | + testAuth(mockRequest as unknown as RequestClient, { |
| 165 | + auth: { accessToken: 'valid-token', refreshToken: '' }, |
| 166 | + settings: { region: Region.NA, advertiserId: '12345', dataSetName: 'my dataset!' } |
| 167 | + } as any) |
| 168 | + ).rejects.toThrow('Dataset Name must start with a letter and can only contain letters, numbers, underscores, or hyphens.') |
| 169 | + }) |
| 170 | + |
| 171 | + it('should pass with a valid dataSetName', async () => { |
| 172 | + await expect( |
| 173 | + testAuth(mockRequest as unknown as RequestClient, { |
| 174 | + auth: { accessToken: 'valid-token', refreshToken: '' }, |
| 175 | + settings: { region: Region.NA, advertiserId: '12345', dataSetName: 'ValidDataset1' } |
| 176 | + } as any) |
| 177 | + ).resolves.toBeDefined() |
| 178 | + expect(mockRequest).toHaveBeenCalled() |
| 179 | + }) |
| 180 | + |
| 181 | + it('should pass with a dataSetName using underscores and hyphens', async () => { |
| 182 | + await expect( |
| 183 | + testAuth(mockRequest as unknown as RequestClient, { |
| 184 | + auth: { accessToken: 'valid-token', refreshToken: '' }, |
| 185 | + settings: { region: Region.NA, advertiserId: '12345', dataSetName: 'Default_Events-2' } |
| 186 | + } as any) |
| 187 | + ).resolves.toBeDefined() |
| 188 | + }) |
| 189 | + |
| 190 | + it('should pass without dataSetName (field is optional)', async () => { |
| 191 | + await expect( |
| 192 | + testAuth(mockRequest as unknown as RequestClient, { |
| 193 | + auth: { accessToken: 'valid-token', refreshToken: '' }, |
| 194 | + settings: { region: Region.NA, advertiserId: '12345' } |
| 195 | + } as any) |
| 196 | + ).resolves.toBeDefined() |
| 197 | + expect(mockRequest).toHaveBeenCalled() |
| 198 | + }) |
| 199 | + |
| 200 | + it('should pass with numeric advertiserId', async () => { |
| 201 | + await expect( |
| 202 | + testAuth(mockRequest as unknown as RequestClient, { |
| 203 | + auth: { accessToken: 'valid-token', refreshToken: '' }, |
| 204 | + settings: { region: Region.NA, advertiserId: '9876543210' } |
| 205 | + } as any) |
| 206 | + ).resolves.toBeDefined() |
| 207 | + }) |
| 208 | + }) |
116 | 209 | }) |
0 commit comments