Skip to content

Think about API abstraction #15

@bahmutov

Description

@bahmutov

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

  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

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
  })
})

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions