-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathuploads.js
More file actions
123 lines (111 loc) · 3.85 KB
/
Copy pathuploads.js
File metadata and controls
123 lines (111 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const assert = require('assert')
const strava = require('../')
const nock = require('nock')
const testHelper = require('./_helper')
const tmp = require('tmp')
const fs = require('fs')
describe('uploads_test', function () {
beforeEach(function () {
// Clean all nock interceptors before each test to ensure isolation
nock.cleanAll()
testHelper.setupMockAuth()
})
afterEach(function () {
nock.cleanAll()
testHelper.cleanupAuth()
})
describe('#post()', function () {
it('should upload a GPX file', async () => {
const tmpFile = tmp.fileSync({ suffix: '.gpx' })
const gpxFilename = tmpFile.name
try {
fs.writeFileSync(gpxFilename, '<?xml version="1.0"?><gpx></gpx>')
const uploadId = '123456'
const activityId = 987654321
// Mock the initial upload POST request
nock('https://www.strava.com')
.post('/api/v3/uploads')
.matchHeader('authorization', 'Bearer test_token')
.once() // Ensure this interceptor is used only once
.reply(201, {
id_str: uploadId,
activity_id: null,
external_id: 'test_external_id',
id: 123456,
error: null,
status: 'Your activity is still being processed.'
})
// Mock the first status check - still processing
nock('https://www.strava.com')
.get(`/api/v3/uploads/${uploadId}`)
.matchHeader('authorization', 'Bearer test_token')
.once()
.reply(200, {
id_str: uploadId,
activity_id: null,
external_id: 'test_external_id',
id: 123456,
error: null,
status: 'Your activity is still being processed.'
})
// Mock the second status check - completed
nock('https://www.strava.com')
.get(`/api/v3/uploads/${uploadId}`)
.matchHeader('authorization', 'Bearer test_token')
.once()
.reply(200, {
id_str: uploadId,
activity_id: activityId,
external_id: 'test_external_id',
id: 123456,
error: null,
status: 'Your activity is ready.'
})
const result = await strava.uploads.post({
activity_type: 'run',
sport_type: 'Run',
data_type: 'gpx',
name: 'test activity',
file: gpxFilename,
maxStatusChecks: 2 // we only mock 2 status responses
})
assert.strictEqual(result.activity_id, activityId)
assert.strictEqual(result.status, 'Your activity is ready.')
} finally {
tmpFile.removeCallback()
}
})
it('should return immediately without maxStatusChecks', async () => {
const tmpFile = tmp.fileSync({ suffix: '.gpx' })
const gpxFilename = tmpFile.name
try {
fs.writeFileSync(gpxFilename, '<?xml version="1.0"?><gpx></gpx>')
const uploadId = '789012'
// Mock the initial upload POST request
nock('https://www.strava.com')
.post('/api/v3/uploads')
.matchHeader('authorization', 'Bearer test_token')
.once() // Ensure this interceptor is used only once
.reply(201, {
id_str: uploadId,
activity_id: null,
external_id: 'test_external_id_2',
id: 789012,
error: null,
status: 'Your activity is still being processed.'
})
const result = await strava.uploads.post({
activity_type: 'run',
sport_type: 'Run',
data_type: 'gpx',
name: 'test activity without callback',
file: gpxFilename
})
assert.strictEqual(result.id_str, uploadId)
assert.strictEqual(result.status, 'Your activity is still being processed.')
} finally {
tmpFile.removeCallback()
}
})
})
})