Day 12 - Failed submit while providing api key correctly

My question:
Hello! I am completely stuck on day 12 since 3 of 5 tests are failing:


I have found similar errors in other sources, but still can’t understand how to fix it:

  1. All the queries work fine (200 OK and valid response)

  2. My api key authorization (key = ‘apikey’, add to query params)

  3. when i am changing authorization this way (key = ‘x-api-key’, add to header):

1 test fails since it can’t find ‘apikey’ any more (pm.expect(response.json().collection.auth.type, ‘check collection authorization’).to.equal(“apikey”))

Thank you for answers in advance!

post editor didn’t allow me to add more screenshots:

  1. successful requests without submission:

The instructions for that day details the following.

Add a collection-level authorization helper to add an API key like x-api-key and {{postman_api_key}} to your request headers.

The key name that goes in the header is always x-api-key. That is what Postman is expecting for authentication. The variable name that you store the key, can be anything you want. However most of the challenges and examples use {{postman_api_key}}

This appears to be the failing test.

 pm.test('get single collection', () => {
    pm.expect(error).to.equal(null);
    pm.expect(response).to.have.property('code', 200);
    pm.expect(response).to.have.property('status', 'OK');
    pm.expect(response.json().collection.auth.type, 'check collection authorization').to.equal("apikey")

    pass += 1
  });
});

It’s doesn’t appear to be checking the variable name.
It’s just checking that the auth type is set to API Key.
Which your screenshots appear to show ok.

You could add a line temporarily to console log the auth type in that code block in the Tests tab to see what is actually being returned.

console.log(response.json().collection.auth.type)

The auth type should inherit throughout all of the requests in that collection including the submit request. Just make sure you don’t have any of the requests with its own authentication, otherwise it will return an array for the auth type and the test will fail.

This request doesn’t work neither


So, as i understood, there is a problem with this expect?

 pm.expect(response.json().collection.auth.type, 'check collection authorization').to.equal("apikey")

Since this statement returns an error

You’ve added this to the wrong block of code.

Add it within the test.

Immediately above the following.

pm.expect(response.json().collection.auth.type, 'check collection authorization').to.equal("apikey")

response is the correct variable, as it’s the sendRequest which is defining it, not the main response which is being assigned to the variable “res” right the top of script (where you added your bit of code).

pm.sendRequest(collRequest, (error, response) => {

Thank you. These are all logs I have after adding this line:

It doesn’t look like its recognising the type (it can’t find the type element, so its returning undefined).

Amend the console logs to retrieve earlier parts of the response, and see if you can see the structure and elements this way.

console.log(response.json());
console.log(response.json().collection);
console.log(response.json().collection.auth);

I’m wondering if auth will return an array.

Are you sure that all of the folders and requests within the collection are all set to “inherit from parent”?

You can also select the request from the GET request in the console log, and look at the response that was retrieved. You should be able to drill down to the auth.type from here. (or use the version that you specifically console log instead). The requests appear to be showing 200 ok, so I’m assuming that the response should be the collection JSON.

Thank you very much for your help! There was a mistake in my environment variable. I have found it due to console.log. Now tests work fine. I will use logging more often now.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.