SAP-samples / SAP-samples/btp-developer-guide-cap

Instructions in add-test-cases.md only working when CAP service is not annotated with @(requires: 'support')

Open
#136 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
No language data
Stars
97
Forks
37
PR merge metrics
No merged PRs in 30d

Description

I'm currently doing a learning journey and I'm at this course https://learning.sap.com/courses/develop-extensions-with-cap-following-the-sap-btp-developer-s-guide

In this course at some point you're adding test-cases to your project and if you let the tests run, all of them should come back positive.
But this is only the case, when the ProcessorService is not annotated with @(requires: 'support').

Using the test.js file of the tutorial:

const cds = require('@sap/cds/lib')
const { default: axios } = require('axios')
const { GET, POST, DELETE, PATCH, expect } = cds.test(__dirname + '../../')

axios.defaults.auth = { username: 'alice' }

jest.setTimeout(11111)

describe('Test The GET Endpoints', () => {
  it('Should check Processor Service', async () => {
    const processorService = await cds.connect.to('ProcessorService')
    const { Incidents } = processorService.entities
    expect(await SELECT.from(Incidents)).to.have.length(4)
  })

  it('Should check Customers', async () => {
    const processorService = await cds.connect.to('ProcessorService')
    const { Customers } = processorService.entities
    expect(await SELECT.from(Customers)).to.have.length(3)
  })

  it('Test Expand Entity Endpoint', async () => {
    const { data } = await GET`/odata/v4/processor/Customers?$select=firstName&$expand=incidents`
    expect(data).to.be.an('object')
  })
})

describe('Draft Choreography APIs', () => {
  let draftId, incidentId

  it('Create an incident ', async () => {
    const { status, statusText, data } = await POST(`/odata/v4/processor/Incidents`, {
      title: 'Urgent attention required !',
      status_code: 'N'
    })
    draftId = data.ID
    expect(status).to.equal(201)
    expect(statusText).to.equal('Created')
  })

  it('+ Activate the draft & check Urgency code as H using custom logic', async () => {
    const response = await POST(
      `/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)/ProcessorService.draftActivate`
    )
    expect(response.status).to.eql(201)
    expect(response.data.urgency_code).to.eql('H')
  })

  it('+ Test the incident status', async () => {
    const {
      status,
      data: { status_code, ID }
    } = await GET(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)`)
    incidentId = ID
    expect(status).to.eql(200)
    expect(status_code).to.eql('N')
  })

  describe('Close Incident and Open it again to check Custom logic', () => {
    it(`Should Close the Incident-${draftId}`, async () => {
      const { status } = await POST(
        `/odata/v4/processor/Incidents(ID=${incidentId},IsActiveEntity=true)/ProcessorService.draftEdit`,
        {
          PreserveChanges: true
        }
      )
      expect(status).to.equal(201)
    })
    it(`Should Close the Incident-${draftId}`, async () => {
      const { status } = await PATCH(`/odata/v4/processor/Incidents(ID=${incidentId},IsActiveEntity=false)`, {
        status_code: 'C'
      })
      expect(status).to.equal(200)
    })
    it('+ Activate the draft & check Status code as C using custom logic', async () => {
      const response = await POST(
        `/odata/v4/processor/Incidents(ID=${incidentId},IsActiveEntity=false)/ProcessorService.draftActivate`
      )
      expect(response.status).to.eql(200)
    })
    it('+ Test the incident status to be closed', async () => {
      const {
        status,
        data: { status_code }
      } = await GET(`/odata/v4/processor/Incidents(ID=${incidentId},IsActiveEntity=true)`)
      expect(status).to.eql(200)
      expect(status_code).to.eql('C')
    })
    describe('should fail to re-open closed incident', () => {
      it(`Should Open Closed Incident-${draftId}`, async () => {
        const { status } = await POST(
          `/odata/v4/processor/Incidents(ID=${incidentId},IsActiveEntity=true)/ProcessorService.draftEdit`,
          {
            PreserveChanges: true
          }
        )
        expect(status).to.equal(201)
      })
      it(`Should re-open the Incident-${draftId} but fail`, async () => {
        const { status } = await PATCH(`/odata/v4/processor/Incidents(ID=${incidentId},IsActiveEntity=false)`, {
          status_code: 'N'
        })
        expect(status).to.equal(200)
      })
      it(' `Should fail to activate draft trying to re-open the incidentt', async () => {
        try {
          const response = await POST(
            `/odata/v4/processor/Incidents(ID=${incidentId},IsActiveEntity=false)/ProcessorService.draftActivate`
          )
        } catch (error) {
          expect(error.response.status).to.eql(500)
          expect(error.response.data.error.message).to.include(`Can't modify a closed incident`)
        }
      })
    })
  })
  it('- Delete the Draft', async () => {
    const response = await DELETE(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)`)
    expect(response.status).to.eql(204)
  })
  it('- Delete the Incident', async () => {
    const response = await DELETE(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)`)
    expect(response.status).to.eql(204)
  })
})

Running the tests with annotation:

 * Service used by support personell, i.e. the incidents' 'processors'.
 */
service ProcessorService {
    entity Incidents as projection on my.Incidents;

    @readonly
    entity Customers as projection on my.Customers;
}

annotate ProcessorService.Incidents with @odata.draft.enabled;
annotate ProcessorService with @(requires: 'support');
/**
 * Service used by administrators to manage customers and incidents.
 */
service AdminService {
    entity Customers as projection on my.Customers;
    entity Incidents as projection on my.Incidents;
}

annotate AdminService with @(requires: 'admin');

Result:

Test Suites: 1 failed, 1 total
Tests:       13 failed, 2 passed, 15 total
Snapshots:   0 total
Time:        3.733 s, estimated 4 s
Ran all test suites matching tests/test.js.
Waiting for the debugger to disconnect...

Running tests without annotation - Result:

Test Suites: 1 passed, 1 total
Tests:       15 passed, 15 total
Snapshots:   0 total
Time:        7.868 s, estimated 25 s
Ran all test suites matching tests/test.js.
Waiting for the debugger to disconnect...

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with add-test-cases.md and the referenced tests/test.js, then run the suite with and without the ProcessorService @(requires: 'support') annotation. Compare the documented setup with the failing authenticated requests; done means the instructions explain or support a passing test run when the annotation is present.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend-api-design, documentation, testing
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.