Postman: How to validate a response to have a correct date format - postman

Just can´t find how to validate the response to be in a date format like: 2014-07-14T21:15:58+00:00
pm.test("Validate dateCreated", function () {
pm.expect(jsonData.data[0].dateCreated == ("YYYY-MM-DDTHH:mm:ss")); // timestamp
});
This is my awfull aproach, it validates the result even if I change operators or the date format in the assertion,I tried with $timestamp, and other examples here, but none seems to work please help, thx
The response body is like this
{
"current_page": 1,
"data": [
{
"rn": "1",
"id": 2,
"mxrId": null,
"dateCreated": "2014-07-14T21:15:58+00:00",
"dateModified": "2018-03-16T21:15:13+00:00",
"svsId": "1",
"token": "08A96E05",
"name": "Original Test",
"clientId": 1675,
"projectId": 1,

And this is what i came with, if you find a better way, please let me know.
let dateFormat = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
pm.test("Validate dateCreated", function () {
pm.expect(jsonData.data[0].dateCreated).to.match(dateFormat);
});

Related

Using Postman How to write a test to check for duplicate ids

I would like to write a test in Postman that validates there are no duplicate values in the array of objects. Here is an example response:
This is my json response :
{
"content": [
{
"id": "88848990-c4c8-4e7d-b708-3e69e684085b",
"name": "UPDATED",
},
{
"id": "42c37e1d-eed3-4f5c-b76a-7b915c05b0bf",
"name": "Swoop ",
},
{
"id": "88848990-c4c8-4e7d-b708-3e69e684085b",
"name": "United Test Airlines",
},
I can see 2 ids that are the same I want to write a test in postman to identify any duplicates in my results. If the test picks up duplicates then it must fail, if it does not pick up any duplicates it should pass. Note I am new to postman api testing.
The short and esay solution might be:
Save all id in an array
Create a set from this array
Compare size of the array and the set. If it equals, then no duplication. If not, there is a duplication.
const res = pm.response.json();
const ids = _.map(res.content, "id");
pm.test("check duplicate id", () => {
const setIds = new Set(ids);
pm.expect(ids.length).eql(setIds.size);
})

Secondary Index not working for Database using #key

I should get the DynamoDb id for Justin. The call doesn't seem to fail. If i console.log(returned) i get an [object Object]. When i try to get to the returned.data.getIdFromUserName.id or returned.data.getIdFromUserName.email (anything else in the table) i get undefined. What am i missing?
Returned data:
{
"data": {
"getIdFromUserName": {
"items": [
{
"id": "3a5a2ks4-f137-41e2-a604-594e0c52a298",
"userName": "Justin",
"firstname": "null",
"weblink": "#JustinTimberlake",
"email": "iuiubiwewe#hotmail.com",
"mobileNum": "+0123456789",
"profilePicURI": "null",
"listOfVideosSeen": null,
"userDescription": "I wanna rock your body, please stay",
"isBlocked": false,
"GridPairs": null
}
],
"nextToken": null
}
}
}
I'd suggest getting a better idea of what console.log(returned) is printing.
Try console.log(JSON.stringify(returned, null, 2)) to inspect what is being returned.
EDIT: The data you're working with looks like this:
{
"data": {
"getIdFromUserName": {
"items": [
{
"id": "3a5a2ks4-f137-41e2-a604-594e0c52a298",
"userName": "Justin",
"firstname": "null",
"weblink": "#JustinTimberlake",
"email": "iuiubiwewe#hotmail.com",
"mobileNum": "+0123456789",
"profilePicURI": "null",
"listOfVideosSeen": null,
"userDescription": "I wanna rock your body, please stay",
"isBlocked": false,
"GridPairs": null
}
],
"nextToken": null
}
}
}
Pay close attention to the structure of that response. Both data and getIdFromUserName are maps. The content of data.getIdFromUserName is an array named items. Therefore, data.getIdFromUserName.items is an array containing the results of your query. You'll need to iterate over that array to get the data you are looking for.
For example, data.getIdFromUserName.items[0].id would be 3a5a2ks4-f137-41e2-a604-594e0c52a298
To access the email it would be data.getIdFromUserName.items[0].email.

Get keys from Json with regex Jmeter

I'm hustling with regex, and trying to get the id's from this body.
But only the id´s in the members list, and not the id in the verified key. :)
To clarify, I'm using Regular Expression Extractor in JMeter
{
"id": "9c40ffca-0f1a-4f93-b068-1f6332707d02", //<--not this
"me": {
"id": "38a2b866-c8a9-424f-a5d4-93b379f080ce", //<--not this
"isMe": true,
"user": {
"verified": {
"id": "257e30f4-d001-47b3-9e7f-5772e591970b" //<--not this
}
}
},
"members": [
{
"id": "88a2b866-c8a9-424f-a5d4-93b379f780ce", //<--this
"isMe": true,
"user": {
"verified": {
"id": "223e30f4-d001-47b3-9e7f-5772e781970b" //<--not this
}
}
},
{
"id": "53cdc218-4784-4e55-a784-72e6a3ffa9bc", //<--this
"isMe": false,
"user": {
"unverified": {
"verification": "XYZ"
}
}
}
]
}
at the moment I have this regex :
("id": )("[\w-]+")
But as you can see here it returns every guid
Any ideas on how to go on?
Thanks in advance.
Since the input data type is JSON, it is recommended to use the JMeter's JSON Path Extractor Plugin.
Once you add it, use the
$.members[*].id
JSON path expression to match all id values of each members in the document that are the top nodes.
If you may have nested memebers, you may get them all using
$..members[*].id
You may test these expressions at https://jsonpath.com/, see a test:

How to get JSONobject from JSONArray in postman

I am trying to automize a registration scenario in postman using test scripts
I have the following JsonArray as a response:
[
{
"id": 1,
"name": "user_A",
"cntkp": "martin",
"company": "kreativ",
"tel": "12345678",
"email": "user_A#gmail.com"
"street": "str. 0001",
"city": "DEF",
}
......
......
......
{
"id": 4,
"name": "user_B",
"cntkp": "martin",
"company": "kreativ",
"tel": "12345678",
"email": "user_B#gmail.com"
"street": "str. 0002",
"city": "NJ",
}
......
......
......
{
"id": 10,
"name": "User_C",
"cntkp": "martin",
"company": "kreativ",
"tel": "12345678",
"email": "user_C#gmail.com"
"street": "str. 0003",
"city": "ABC",
}
......
]
the array length can be dynamic and changed (in this sample is 10) and want to find the object with special email (somewhere in the array) and then get the ID from that object and make the assertion based on JsonData from this object (catch elements e.g. check name).
how can I do that?
thanks for the support.
I send a GETrequest to get all Data from Registration DB.
as response I get a JsonArray
from Json Array I need the specific Object for the assertion (e.g. object with email user_B in sample) .
I know my Email address and base on it I have to findout the ID from Object .
I can do it when I know which ID is my ID but in case it is dynamic I don't know how to search an array for it in postman to get ID
For example, to assert the company name
pm.expect(jsonData[0].company).to.equal(pm.environment.get("regDB_new_company"))
but if I dont know the ID ( only know my email) I have first to find out the ID of Object then I can asser it.
e.g.
in this case first, find the object with email "user_B#gmail.com"
then from that object get ID element (in this case 4)
then I want to assert for all data from the object
Thanks Danny, I found the solution
var arr = pm.response.json()
for(i = 0; i < arr.length; i++) {
if (arr[i].email == "userB#gmail.com") {
pm.environment.set("personID", arr[i].id)
}
}

How to get List<Object> with RestTemplate(SpringBoot)

I want to get: List<User>. I have endpoint(GET) for getting users. It gets:
[
{
"id": "d71dcbca-54f3-4b19-aec4-3776bfe34730",
"name": "test",
"surname": "test",
"login": "test",
"password": "-26104458",
"email": "test",
"role": "user"
}
]
I try get getting this list with using rest template:
ResponseEntity<User[]> responseEntity = rest.getForEntity(my-endpoint, User[].class);
return Arrays.asList(responseEntity.getBody());
But I get this errror:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type
Question: How to get List<User> with using rest template?
Maybe you want to try this approach:
ResponseEntity<List<User>> responseEntity = rest.exchange(
"your-endpoint",
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<User>>() {
});
See also https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/#rest-resttemplate