bahmutov / bahmutov/cy-api

Think about API abstraction

Open
#15 0 comments 1 reaction 1 assignee Claimed by @LeJeanbono View on GitHub
Dominant language
TypeScript
Stars
210
Forks
26
PR merge metrics
No merged PRs in 30d

Description

Common use case: need to login, then perform authorized API calls

```js
it('user cannot follow themselves', () => {
const user = Cypress.env('user')
cy.getLoginToken(user).then(token => {
const apiUrl = Cypress.env('apiUrl')
cy.api({
method: 'POST',
url: `${apiUrl}/api/profiles/${user.username}/follow`,
headers: {
authorization: `Token ${token}`
},
failOnStatusCode: false // we expect failure
})
```

It would be nice if we could create the API client once (using `before`) and then could make the calls. Something like

```js
let apiClient
before(() => {
const user = Cypress.env('user')
cy.getLoginToken(user).then(token => {
const apiUrl = Cypress.env('apiUrl')
apiClient = cy.apiClient({
baseUrl: `${apiUrl}/api`,
headers: {
authorization: `Token ${token}`
}
})
})
})

it('user cannot follow themselves', () => {
apiClient({
method: 'POST',
url: `/profiles/${user.username}/follow`,
failOnStatusCode: false // we expect failure
})
})
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.