-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
79 lines (68 loc) · 2.79 KB
/
index.js
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
const HEADERS = {
"Authorization": Cypress.env('pipedreamBearer'),
}
const dir = Cypress.env('pipedreamFolderPath') ? Cypress.env('pipedreamFolderPath') : 'cypress/fixtures/sms_response'
const maxRetries = Cypress.env('pipedreamMaxRetries') ? Cypress.env('pipedreamMaxRetries') : 12
const fileName = Cypress.env('pipedreamFileName') ? Cypress.env('pipedreamFileName') : 'message.json'
const baseUrl = "https://api.pipedream.com/v1/sources/" + Cypress.env('pipedreamSourceID')
const writeSMS = function (body) {
cy.writeFile(dir + '/' + fileName, JSON.stringify(body))
}
const getMessage = (age, count = 0) => {
const options = {
url: baseUrl + "/event_summaries",
headers: HEADERS,
}
// Loop checking every 1000ms for a max of pipedreamMaxRetries times/seconds
if (count === maxRetries) {
throw new Error(`CYPRESS-PIPEDREAM-PLUGIN | No message received in ${maxRetries}seconds, please check https://pipedream.com/sources/${Cypress.env('pipedreamSourceID')}`)
}
cy.request(options)
.then((res) => {
let newMessageAvailable = false
let SMSBody
if (res.body.data.length !== 0) {
newMessageAvailable = true
expect(res.status).to.be.eq(200)
expect(res.statusText).to.be.eq("OK")
expect(res.body.data).to.not.be.empty
if (age === 'newest') {
SMSBody = res.body.data[0].event.Body
} else if (age === 'oldest') {
SMSBody = res.body.data[res.body.data.length - 1].event.Body
}
cy.log('📧 ' + SMSBody)
writeSMS(SMSBody)
}
if (newMessageAvailable === false) {
cy.wait(1200)
getMessage(age, ++count)
}
});
}
if (Cypress.env('pipedreamBearer') && Cypress.env('pipedreamSourceID')) {
Cypress.Commands.add('getOldestMessage', () => {
getMessage('oldest');
})
Cypress.Commands.add('getNewestMessage', () => {
getMessage('newest');
})
Cypress.Commands.add('clearMessagesHistory', () => {
const options = {
method: "DELETE",
url: baseUrl + "/events",
headers: HEADERS,
}
cy.request(options).then(res => {
expect(res.body).to.be.eq(" ")
expect(res.status).to.be.eq(202)
})
})
Cypress.Commands.add('clearHistorySendSmsAndGetSMS', () => {
cy.clearMessagesHistory()
cy.get('[data-cy="send-sms"]', { timeout: 120000 }).click() // Command to send the SMS from the frontend
cy.getNewestMessage()
})
} else {
throw new Error(`CYPRESS-PIPEDREAM-PLUGIN | Missing environment variables: env('pipedreamBearer') & env('pipedreamSourceID') needed`)
}