Skip to content

Commit ee5cbcc

Browse files
fix taguchi destination-actions bugs and unit tests
1 parent 0f25b08 commit ee5cbcc

File tree

15 files changed

+1333
-237
lines changed

15 files changed

+1333
-237
lines changed
Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,98 @@
11
import nock from 'nock'
2-
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
2+
import { createTestIntegration } from '@segment/actions-core'
33
import Definition from '../index'
44

55
const testDestination = createTestIntegration(Definition)
66

77
describe('Taguchi', () => {
8+
beforeEach(() => {
9+
nock.cleanAll()
10+
})
11+
812
describe('testAuthentication', () => {
9-
it('should validate authentication inputs', async () => {
10-
nock('https://your.destination.endpoint').get('*').reply(200, {})
13+
it('should validate authentication with correct credentials', async () => {
14+
// Mock the test endpoint (URL with /prod replaced by /test)
15+
nock('https://api.taguchi.com.au')
16+
.post('/test/subscriber')
17+
.reply(200, [
18+
{
19+
code: 200,
20+
name: 'Success',
21+
description: 'Authentication successful'
22+
}
23+
])
1124

12-
// This should match your authentication.fields
13-
const authData = {}
25+
const authData = {
26+
apiKey: 'test-api-key',
27+
integrationURL: 'https://api.taguchi.com.au/prod',
28+
organizationId: '123'
29+
}
1430

1531
await expect(testDestination.testAuthentication(authData)).resolves.not.toThrowError()
1632
})
33+
34+
it('should handle authentication failure', async () => {
35+
// Mock authentication failure
36+
nock('https://api.taguchi.com.au').post('/test/subscriber').reply(401, {
37+
code: 401,
38+
name: 'Unauthorized',
39+
description: 'Invalid API key'
40+
})
41+
42+
const authData = {
43+
apiKey: 'invalid-api-key',
44+
integrationURL: 'https://api.taguchi.com.au/prod',
45+
organizationId: '123'
46+
}
47+
48+
await expect(testDestination.testAuthentication(authData)).rejects.toThrowError()
49+
})
50+
51+
it('should handle invalid integration URL', async () => {
52+
// Mock network error for invalid URL
53+
nock('https://invalid.endpoint.com')
54+
.post('/test/subscriber')
55+
.replyWithError('getaddrinfo ENOTFOUND invalid.endpoint.com')
56+
57+
const authData = {
58+
apiKey: 'test-api-key',
59+
integrationURL: 'https://invalid.endpoint.com/prod',
60+
organizationId: '123'
61+
}
62+
63+
await expect(testDestination.testAuthentication(authData)).rejects.toThrowError()
64+
})
65+
66+
it('should replace /prod with /test in URL', async () => {
67+
// Verify that the test endpoint is called correctly
68+
const scope = nock('https://api.taguchi.com.au')
69+
.post('/test/subscriber', (body) => {
70+
// Verify the request body structure
71+
expect(Array.isArray(body)).toBe(true)
72+
expect(body[0]).toHaveProperty('profile')
73+
expect(body[0].profile).toHaveProperty('organizationId', 123)
74+
expect(body[0].profile).toHaveProperty('ref', 'test-connection')
75+
expect(body[0].profile).toHaveProperty('firstname', 'Test')
76+
return true
77+
})
78+
.reply(200, [
79+
{
80+
code: 200,
81+
name: 'Success',
82+
description: 'Test connection successful'
83+
}
84+
])
85+
86+
const authData = {
87+
apiKey: 'test-api-key',
88+
integrationURL: 'https://api.taguchi.com.au/prod',
89+
organizationId: '123'
90+
}
91+
92+
await testDestination.testAuthentication(authData)
93+
94+
// Verify that the correct endpoint was called
95+
expect(scope.isDone()).toBe(true)
96+
})
1797
})
1898
})
Lines changed: 177 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,202 @@
1+
import nock from 'nock'
12
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
2-
import { generateTestData } from '../../../lib/test-data'
33
import destination from '../index'
4-
import nock from 'nock'
54

65
const testDestination = createTestIntegration(destination)
7-
const destinationSlug = 'actions-taguchi'
86

9-
describe(`Testing snapshot for ${destinationSlug} destination:`, () => {
10-
for (const actionSlug in destination.actions) {
11-
it(`${actionSlug} action - required fields`, async () => {
12-
const seedName = `${destinationSlug}#${actionSlug}`
13-
const action = destination.actions[actionSlug]
14-
const [eventData, settingsData] = generateTestData(seedName, destination, action, true)
7+
describe('Taguchi Destination Snapshots', () => {
8+
beforeEach(() => {
9+
nock.cleanAll()
10+
})
1511

16-
nock(/.*/).persist().get(/.*/).reply(200)
17-
nock(/.*/).persist().post(/.*/).reply(200)
18-
nock(/.*/).persist().put(/.*/).reply(200)
12+
describe('syncAudience action', () => {
13+
it('should match snapshot with required fields', async () => {
14+
nock('https://api.taguchi.com.au')
15+
.post('/subscriber')
16+
.reply(200, [{ code: 200, name: 'Success', description: 'Subscriber processed' }])
1917

2018
const event = createTestEvent({
21-
properties: eventData
19+
type: 'identify',
20+
userId: 'test-user-123',
21+
traits: {
22+
23+
first_name: 'John',
24+
last_name: 'Doe'
25+
}
2226
})
2327

24-
const responses = await testDestination.testAction(actionSlug, {
25-
event: event,
26-
mapping: event.properties,
27-
settings: settingsData,
28-
auth: undefined
28+
const { data } = await testDestination.testAction('syncAudience', {
29+
event,
30+
settings: {
31+
apiKey: 'test-api-key',
32+
integrationURL: 'https://api.taguchi.com.au',
33+
organizationId: '123'
34+
},
35+
mapping: {
36+
identifiers: {
37+
ref: event.userId,
38+
email: event.traits?.email
39+
},
40+
traits: {
41+
firstname: event.traits?.first_name,
42+
lastname: event.traits?.last_name
43+
},
44+
timestamp: event.timestamp
45+
}
2946
})
3047

31-
const request = responses[0].request
32-
const rawBody = await request.text()
48+
expect(data).toMatchSnapshot()
49+
})
3350

34-
try {
35-
const json = JSON.parse(rawBody)
36-
expect(json).toMatchSnapshot()
37-
return
38-
} catch (err) {
39-
expect(rawBody).toMatchSnapshot()
40-
}
51+
it('should match snapshot with all fields', async () => {
52+
nock('https://api.taguchi.com.au')
53+
.post('/subscriber')
54+
.reply(200, [{ code: 200, name: 'Success', description: 'Subscriber processed' }])
4155

42-
expect(request.headers).toMatchSnapshot()
43-
})
56+
const event = createTestEvent({
57+
type: 'identify',
58+
userId: 'test-user-123',
59+
traits: {
60+
61+
first_name: 'John',
62+
last_name: 'Doe',
63+
title: 'Mr',
64+
birthday: '1990-01-01T00:00:00.000Z',
65+
street: '123 Main St',
66+
city: 'Test City',
67+
state: 'Test State',
68+
country: 'Test Country',
69+
postal_code: '12345',
70+
phone: '555-1234',
71+
gender: 'Male'
72+
}
73+
})
74+
75+
const { data } = await testDestination.testAction('syncAudience', {
76+
event,
77+
settings: {
78+
apiKey: 'test-api-key',
79+
integrationURL: 'https://api.taguchi.com.au',
80+
organizationId: '123'
81+
},
82+
mapping: {
83+
identifiers: {
84+
ref: event.userId,
85+
email: event.traits?.email
86+
},
87+
traits: {
88+
title: event.traits?.title,
89+
firstname: event.traits?.first_name,
90+
lastname: event.traits?.last_name,
91+
dob: event.traits?.birthday,
92+
address: event.traits?.street,
93+
suburb: event.traits?.city,
94+
state: event.traits?.state,
95+
country: event.traits?.country,
96+
postcode: event.traits?.postal_code,
97+
phone: event.traits?.phone,
98+
gender: event.traits?.gender
99+
},
100+
subscribeLists: ['123', '456'],
101+
unsubscribeLists: ['789'],
102+
timestamp: event.timestamp
103+
}
104+
})
44105

45-
it(`${actionSlug} action - all fields`, async () => {
46-
const seedName = `${destinationSlug}#${actionSlug}`
47-
const action = destination.actions[actionSlug]
48-
const [eventData, settingsData] = generateTestData(seedName, destination, action, false)
106+
expect(data).toMatchSnapshot()
107+
})
108+
})
49109

50-
nock(/.*/).persist().get(/.*/).reply(200)
51-
nock(/.*/).persist().post(/.*/).reply(200)
52-
nock(/.*/).persist().put(/.*/).reply(200)
110+
describe('syncEvent action', () => {
111+
it('should match snapshot with required fields', async () => {
112+
nock('https://api.taguchi.com.au')
113+
.post('/subscriber')
114+
.reply(200, [{ code: 200, name: 'Success', description: 'Event processed' }])
53115

54116
const event = createTestEvent({
55-
properties: eventData
117+
type: 'track',
118+
event: 'Order Completed',
119+
userId: 'test-user-123',
120+
properties: {
121+
total: 123.5,
122+
products: [{ sku: '1290W', price: 123.5 }],
123+
124+
}
125+
})
126+
127+
const { data } = await testDestination.testAction('syncEvent', {
128+
event,
129+
settings: {
130+
apiKey: 'test-api-key',
131+
integrationURL: 'https://api.taguchi.com.au',
132+
organizationId: '123'
133+
},
134+
mapping: {
135+
target: {
136+
ref: event.userId,
137+
email: event.properties?.email
138+
},
139+
eventType: 'p',
140+
eventData: {
141+
total: event.properties?.total,
142+
products: event.properties?.products
143+
}
144+
}
56145
})
57146

58-
const responses = await testDestination.testAction(actionSlug, {
59-
event: event,
60-
mapping: event.properties,
61-
settings: settingsData,
62-
auth: undefined
147+
expect(data).toMatchSnapshot()
148+
})
149+
150+
it('should match snapshot with all fields', async () => {
151+
nock('https://api.taguchi.com.au')
152+
.post('/subscriber')
153+
.reply(200, [{ code: 200, name: 'Success', description: 'Event processed' }])
154+
155+
const event = createTestEvent({
156+
type: 'track',
157+
event: 'Order Completed',
158+
userId: 'test-user-123',
159+
properties: {
160+
total: 123.5,
161+
products: [
162+
{
163+
sku: '1290W',
164+
price: 123.5,
165+
quantity: 1,
166+
name: 'Test Product',
167+
category: 'Electronics'
168+
}
169+
],
170+
currency: 'USD',
171+
order_id: 'order-123',
172+
173+
}
63174
})
64175

65-
const request = responses[0].request
66-
const rawBody = await request.text()
176+
const { data } = await testDestination.testAction('syncEvent', {
177+
event,
178+
settings: {
179+
apiKey: 'test-api-key',
180+
integrationURL: 'https://api.taguchi.com.au',
181+
organizationId: '123'
182+
},
183+
mapping: {
184+
target: {
185+
ref: event.userId,
186+
email: event.properties?.email
187+
},
188+
isTest: false,
189+
eventType: 'p',
190+
eventData: {
191+
total: event.properties?.total,
192+
products: event.properties?.products,
193+
currency: event.properties?.currency,
194+
order_id: event.properties?.order_id
195+
}
196+
}
197+
})
67198

68-
try {
69-
const json = JSON.parse(rawBody)
70-
expect(json).toMatchSnapshot()
71-
return
72-
} catch (err) {
73-
expect(rawBody).toMatchSnapshot()
74-
}
199+
expect(data).toMatchSnapshot()
75200
})
76-
}
201+
})
77202
})

0 commit comments

Comments
 (0)