Best JavaScript code snippet using wpt
customer.spec.js
Source:customer.spec.js
1import Customer from '../../lib/endpoints/customer/customer.js';2import { getApiClientWithSession } from '../../lib/flow.js';3describe('Customer resource', () => {4 let apiClient;5 beforeEach(async () => {6 apiClient = await getApiClientWithSession();7 });8 test('Customer GET all', async () => {9 const getCustomerResponse = await apiClient.customer().get();10 expect(getCustomerResponse.status).toEqual(200);11 expect(getCustomerResponse.data.length).toBeGreaterThan(0);12 expect(Customer.validateGetAllResponse(getCustomerResponse.data)).toEqual(true);13 });14 test.each([15 [{ name: 'Krista Rohan' }, c => c.name === 'Krista Rohan'],16 [{ age_gte: 90 }, c => c.age >= 90],17 [{ gender: 'Female', age_lte: 25}, c => c.age <= 25 && c.gender === 'Female'],18 [{ email_like: 'gmail' }, c => c.email.includes('gmail')],19 [{ name: 'Zero results' }, c => c.name.includes('Zero results')],20 ])('Customer GET filtered', async (filters, assertFunction) => {21 const getCustomerResponse = await apiClient.customer().get({ filters });22 expect(getCustomerResponse.data.length).toEqual(getCustomerResponse.data.filter(assertFunction).length);23 });24 test('Customer GET by id', async () => {25 const getCustomerResponse = await apiClient.customer().get({ id: '618a9f7505663884a4fa0859' });26 expect(getCustomerResponse.status).toEqual(200);27 expect(getCustomerResponse.data).toMatchObject({28 id: '618a9f7505663884a4fa0859',29 age: 22,30 name: 'Robles Johnston',31 gender: 'Male',32 company: 'BOSTONIC',33 email: 'roblesjohnston@bostonic.com',34 phone: '871-496-200',35 address: '533 Hastings Street, Nord, Wyoming, 7009',36 credits: [ { bank: 'National Bank', amount: 1518 } ]37 });38 });39 test.each([40 'invalid123',41 '!@#$%^&*(((',42 '-123',43 ])('Customer GET by invalid id', async (id) => {44 const getCustomerResponse = await apiClient.customer().get({ id });45 expect(getCustomerResponse.status).toEqual(404);46 });47 test('Customer POST', async () => {48 const postCustomerResponse = await apiClient.customer().post();49 const expectedData = JSON.parse(postCustomerResponse.config.data);50 expect(postCustomerResponse.status).toEqual(201);51 expect(postCustomerResponse.data).toMatchObject(expectedData);52 expect(Customer.validatePostResponse(postCustomerResponse.data)).toEqual(true);53 const getClientResponse = await apiClient.customer().get({ id: postCustomerResponse.data.id });54 expect(getClientResponse.data).toMatchObject(expectedData);55 });56 test.each([57 [["age", "name", "gender", "email", "phone", "address", "credits"], "Missing param or invalid value: age, name, gender, email, phone, address, credits"],58 [["gender"], "Missing param or invalid value: gender"]59 ])('Customer POST required params missing', async (missingParams, expectedMessage) => {60 const data = Customer.getDefaultData();61 missingParams.forEach(param => delete data[param]);62 const postCustomerResponse = await apiClient.customer().post({ data, rawData: true });63 const expectedData = {64 "status": 400,65 "message": expectedMessage66 }67 expect(postCustomerResponse.status).toEqual(expectedData.status);68 expect(postCustomerResponse.data).toEqual(expectedData);69 });70 71 test('Customer POST client already exists', async () => {72 const postCustomerResponse = await apiClient.customer().post();73 const addedCustomerData = JSON.parse(postCustomerResponse.config.data);74 const postAgainCustomerResponse = await apiClient.customer().post({ data: addedCustomerData });75 const expectedData = {76 "status": 409,77 "message": "Customer already exists"78 }79 expect(postAgainCustomerResponse.status).toEqual(expectedData.status);80 expect(postAgainCustomerResponse.data).toMatchObject(expectedData);81 });82 test.each([83 [{naame: 'John'}],84 [{Agge: 123, UknownedParameter: 'xyz'}],85 ])('Customer POST uknowned parameters', async (invalidParams) => {86 const postCustomerResponse = await apiClient.customer().post({ data: invalidParams });87 expect(postCustomerResponse.status).toEqual(201);88 const getCustomerResponse = await apiClient.customer().get({ id: postCustomerResponse.data.id });89 90 for (const invalidParam of Object.keys(invalidParams)) {91 expect(getCustomerResponse.data).not.toHaveProperty(invalidParam);92 }93 });94 test.each([95 [{age: 'John'}],96 [{age: 'John', name: '1', gender: 'Gender', company: 123, email: 'abc', phone: '9-2-11', address: 678, credits: 'credits'}],97 ])('Customer POST invalid parameters values', async (invalidParamsValues) => {98 const postCustomerResponse = await apiClient.customer().post({ data: invalidParamsValues });99 const expectedData = {100 "status": 400,101 "message": `Missing param or invalid value: ${Object.keys(invalidParamsValues).join(', ')}`102 }103 expect(postCustomerResponse.status).toEqual(expectedData.status);104 expect(postCustomerResponse.data).toMatchObject(expectedData);105 });106 test('Customer PUT', async () => {107 const postCustomerResponse = await apiClient.customer().post();108 const putCustomerResponse = await apiClient.customer().put({ id: postCustomerResponse.data.id });109 const expectedData = JSON.parse(putCustomerResponse.config.data);110 expect(putCustomerResponse.status).toEqual(200);111 expect(putCustomerResponse.data).toMatchObject(expectedData);112 expect(Customer.validatePutResponse(putCustomerResponse.data)).toEqual(true);113 const getCustomerResponse = await apiClient.customer().get({ id: postCustomerResponse.data.id });114 expect(getCustomerResponse.data).toMatchObject(expectedData);115 });116 test('Customer PUT invalid id', async () => {117 const putCustomerResponse = await apiClient.customer().put({ id: 'non-existing-id' });118 expect(putCustomerResponse.status).toEqual(404);119 });120 test.each([121 [["age", "name", "gender", "email", "phone", "address", "credits"], "Missing param or invalid value: age, name, gender, email, phone, address, credits"],122 [["gender"], "Missing param or invalid value: gender"]123 ])('Customer PUT required params missing', async (missingParams, expectedMessage) => {124 const data = Customer.getDefaultData();125 missingParams.forEach(param => delete data[param]);126 const postCustomerResponse = await apiClient.customer().post();127 const putCustomerResponse = await apiClient.customer().put({ id: postCustomerResponse.data.id, data, rawData: true });128 const expectedData = {129 "status": 400,130 "message": expectedMessage131 }132 expect(putCustomerResponse.status).toEqual(expectedData.status);133 expect(putCustomerResponse.data).toEqual(expectedData);134 });135 test('Customer PUT client already exists', async () => {136 const postCustomerResponse = await apiClient.customer().post();137 const existingClientData = (await apiClient.customer().get()).data[0];138 delete existingClientData.id;139 const putCustomerResponse = await apiClient.customer().put({ id: postCustomerResponse.data.id, data: existingClientData });140 const expectedData = {141 "status": 409,142 "message": "Customer already exists"143 }144 expect(putCustomerResponse.status).toEqual(expectedData.status);145 expect(putCustomerResponse.data).toMatchObject(expectedData);146 });147 test.each([148 [{naame: 'John'}],149 [{Agge: 123, UknownedParameter: 'xyz'}],150 ])('Customer PUT uknowned parameters', async (invalidParams) => {151 const postCustomerResponse = await apiClient.customer().post();152 const putCustomerResponse = await apiClient.customer().put({ id: postCustomerResponse.data.id, data: invalidParams });153 expect(putCustomerResponse.status).toEqual(200);154 const getCustomerResponse = await apiClient.customer().get({ id: postCustomerResponse.data.id });155 156 for (const invalidParam of Object.keys(invalidParams)) {157 expect(getCustomerResponse.data).not.toHaveProperty(invalidParam);158 }159 });160 test.each([161 [{age: 'John'}],162 [{age: 'John', name: '1', gender: 'Gender', company: 123, email: 'abc', phone: '9-2-11', address: 678, credits: 'credits'}],163 ])('Customer PUT invalid parameters values', async (invalidParamsValues) => {164 const postCustomerResponse = await apiClient.customer().post();165 const putCustomerResponse = await apiClient.customer().put({ id: postCustomerResponse.data.id, data: invalidParamsValues });166 const expectedData = {167 "status": 400,168 "message": `Missing param or invalid value: ${Object.keys(invalidParamsValues).join(', ')}`169 }170 expect(putCustomerResponse.status).toEqual(expectedData.status);171 expect(putCustomerResponse.data).toMatchObject(expectedData);172 });173 test('Customer PATCH', async () => {174 const postCustomerResponse = await apiClient.customer().post();175 const patchCustomerResponse = await apiClient.customer().patch({ id: postCustomerResponse.data.id, data: { name: Customer.getDefaultData().name } });176 const expectedData = JSON.parse(patchCustomerResponse.config.data);177 expect(patchCustomerResponse.status).toEqual(200);178 expect(patchCustomerResponse.data).toMatchObject(expectedData);179 expect(Customer.validatePutResponse(patchCustomerResponse.data)).toEqual(true);180 const getCustomerResponse = await apiClient.customer().get({ id: postCustomerResponse.data.id });181 expect(getCustomerResponse.data).toMatchObject(expectedData);182 });183 test('Customer PATCH invalid id', async () => {184 const patchCustomerResponse = await apiClient.customer().patch({ id: 'non-existing-id' });185 expect(patchCustomerResponse.status).toEqual(404);186 });187 test('Customer PATCH client already exists', async () => {188 const postCustomerResponse = await apiClient.customer().post();189 const existingClientData = (await apiClient.customer().get()).data[0];190 delete existingClientData.id;191 const patchCustomerResponse = await apiClient.customer().patch({ id: postCustomerResponse.data.id, data: { name: existingClientData.name } });192 const expectedData = {193 "status": 409,194 "message": "Customer already exists"195 }196 expect(patchCustomerResponse.status).toEqual(expectedData.status);197 expect(patchCustomerResponse.data).toMatchObject(expectedData);198 });199 test.each([200 [{naame: 'John'}],201 [{Agge: 123, UknownedParameter: 'xyz'}],202 ])('Customer PATCH uknowned parameters', async (invalidParams) => {203 const postCustomerResponse = await apiClient.customer().post();204 const patchCustomerResponse = await apiClient.customer().patch({ id: postCustomerResponse.data.id, data: invalidParams });205 expect(patchCustomerResponse.status).toEqual(200);206 const getCustomerResponse = await apiClient.customer().get({ id: postCustomerResponse.data.id });207 208 for (const invalidParam of Object.keys(invalidParams)) {209 expect(getCustomerResponse.data).not.toHaveProperty(invalidParam);210 }211 });212 test.each([213 [{age: 'John'}],214 [{age: 'John', name: '1', gender: 'Gender', email: 'abc', phone: '9-2-11', address: 678, credits: 'credits'}],215 ])('Customer PATCH invalid parameters values', async (invalidParamsValues) => {216 const postCustomerResponse = await apiClient.customer().post();217 const patchCustomerResponse = await apiClient.customer().patch({ id: postCustomerResponse.data.id, data: invalidParamsValues });218 const expectedData = {219 "status": 400,220 "message": `Missing param or invalid value: ${Object.keys(invalidParamsValues).join(', ')}`221 }222 expect(patchCustomerResponse.status).toEqual(expectedData.status);223 expect(patchCustomerResponse.data).toMatchObject(expectedData);224 });225 test('Customer DELETE', async () => {226 const customerId = (await apiClient.customer().post()).data.id;227 const deleteCustomerResponse = await apiClient.customer().delete({ id: customerId });228 expect(deleteCustomerResponse.status).toEqual(200);229 const getClientResponse = await apiClient.customer().get({ id: customerId });230 expect(getClientResponse.status).toEqual(404);231 });232 test('Customer DELETE invalid id', async () => {233 const deleteCustomerResponse = await apiClient.customer().delete({ id: 'InvalidID' });234 expect(deleteCustomerResponse.status).toEqual(404);235 });...
teacherStudent.test.js
Source:teacherStudent.test.js
1const request = require("supertest");2const app = require("../app");3describe("/api", () => {4 describe("/register", () => {5 it("POST should register the teacher and student input with status 204 without showing any information", async () => {6 const expectedData = {7 teacher: "teacherken@gmail.com",8 students: ["studentjon@example.com", "studenthon@example.com"],9 };10 const response = await request(app)11 .post("/api/register")12 .send(expectedData)13 .expect(204);14 expect(response).not.toMatchObject(expectedData);15 });16 it("POST should throw error if there are empty inputs", async () => {17 const expectedData = {18 students: ["studentjon@example.com", "studenthon@example.com"],19 };20 const errorMessage = { error: "Missing teacher or student input" };21 const { body: error } = await request(app)22 .post("/api/register")23 .send(expectedData)24 .expect(422);25 expect(error).toMatchObject(errorMessage);26 });27 it("POST should throw error if there are more than one teacher inputs", async () => {28 const expectedData = {29 teacher: ["teacherken@gmail.com", "teacherben@gmail.com"],30 students: ["studentjon@example.com", "studenthon@example.com"],31 };32 const errorMessage = { error: "Only one teacher input allowed." };33 const { body: error } = await request(app)34 .post("/api/register")35 .send(expectedData)36 .expect(422);37 expect(error).toMatchObject(errorMessage);38 });39 });40 describe("/commonstudents", () => {41 it("GET should retrieve students information registered to the teacher if there is only one teacher", async () => {42 const sampleTeacherQuery = "teacher=teacherken%40gmail.com";43 const sampleTeacher = "teacherken@gmail.com";44 const sampleData = {45 teacher: sampleTeacher,46 students: [47 "studentjon@example.com",48 "studenthon@example.com",49 "student_only_under_teacher_ken@gmail.com",50 ],51 };52 const response = await request(app)53 .get(`/api/commonstudents?${sampleTeacherQuery}`)54 .send()55 .expect(200);56 expect(response.body).toMatchObject(sampleData);57 });58 it("GET should retrieve common students information registered multiple teachers", async () => {59 const sampleTeacher =60 "teacher=teacherken%40gmail.com&teacher=teacherben%40gmail.com";61 const sampleData = {62 students: ["studentjon@example.com", "studenthon@example.com"],63 };64 const response = await request(app)65 .get(`/api/commonstudents?${sampleTeacher}`)66 .send()67 .expect(200);68 expect(response.body).toMatchObject(sampleData);69 });70 it("GET should throw error if there is no teacher input", async () => {71 const errorMessage = { error: "No teacher input" };72 const { body: error } = await request(app)73 .get(`/api/commonstudents?teacher=`)74 .send()75 .expect(422);76 expect(error).toMatchObject(errorMessage);77 });78 it("GET should throw error if single teacher input is unavailable or invalid", async () => {79 const errorMessage = { error: "Teacher input unavailable or invalid." };80 const { body: error } = await request(app)81 .get(`/api/commonstudents?teacher=wrong%40email.com`)82 .send()83 .expect(422);84 expect(error).toMatchObject(errorMessage);85 });86 it("GET should throw error if one of the teacher inputs is invalid", async () => {87 const sampleTeachers =88 "teacher=teacherken%40gmail.com&teacher=wrong%teacher.com";89 const errorMessage = { error: "Teacher input unavailable or invalid." };90 const { body: error } = await request(app)91 .get(`/api/commonstudents?${sampleTeachers}`)92 .send()93 .expect(422);94 expect(error).toMatchObject(errorMessage);95 });96 });97 describe("/suspend", () => {98 it("POST should return a status 204 when a student is suspended", async () => {99 const expectedData = {100 student: "studentjon@example.com",101 };102 const response = await request(app)103 .post("/api/suspend")104 .send(expectedData)105 .expect(204);106 expect(response).not.toMatchObject(expectedData);107 });108 it("POST should throw error when missing student input", async () => {109 const expectedData = {};110 const errorMessage = { error: "Missing student input." };111 const { body: error } = await request(app)112 .post("/api/suspend")113 .send(expectedData)114 .expect(422);115 expect(error).toMatchObject(errorMessage);116 });117 it("POST should throw error when student to suspend does not exist", async () => {118 const expectedData = { student: "wrong" };119 const errorMessage = { error: "Student invalid or does not exist." };120 const { body: error } = await request(app)121 .post("/api/suspend")122 .send(expectedData)123 .expect(422);124 expect(error).toMatchObject(errorMessage);125 });126 it("POST should throw error when more than one student is in input to suspend", async () => {127 const expectedData = {128 student: ["studentjon@example.com", "studenthon@example.com"],129 };130 const errorMessage = { error: "Only one student input allowed." };131 const { body: error } = await request(app)132 .post("/api/suspend")133 .send(expectedData)134 .expect(422);135 expect(error).toMatchObject(errorMessage);136 });137 });138 describe("/retrievefornotifications", () => {139 it("POST should retrieve not suspended students, inclusive of mentions and without duplicates", async () => {140 const expectedData = {141 teacher: "teacherben@gmail.com",142 notification:143 "Hello students! @student_only_under_teacher_ken@gmail.com",144 };145 const expectedResult = {146 recipients: [147 "studenthon@example.com",148 "student_only_under_teacher_ken@gmail.com",149 ],150 };151 const response = await request(app)152 .post("/api/retrievefornotifications")153 .send(expectedData)154 .expect(200);155 expect(response.body).toMatchObject(expectedResult);156 });157 it("POST should throw error when input is empty", async () => {158 const expectedData = {};159 const errorMessage = { error: "Missing teacher or notification input." };160 const { body: error } = await request(app)161 .post("/api/retrievefornotifications")162 .send(expectedData)163 .expect(422);164 expect(error).toMatchObject(errorMessage);165 });166 it("POST should throw error when teacher input is unavailable", async () => {167 const expectedData = {168 teacher: "wrong@teacher.com",169 notification:170 "Hello students! @student_only_under_teacher_ken@gmail.com",171 };172 const errorMessage = { error: "Teacher input unavailable or invalid." };173 const { body: error } = await request(app)174 .post("/api/retrievefornotifications")175 .send(expectedData)176 .expect(422);177 expect(error).toMatchObject(errorMessage);178 });179 it("POST should throw error when mentioned students are unavailable", async () => {180 const expectedData = {181 teacher: "teacherben@gmail.com",182 notification: "Hello students! @wrongStudent@gmail.com",183 };184 const errorMessage = { error: "Not all students mentioned are found." };185 const { body: error } = await request(app)186 .post("/api/retrievefornotifications")187 .send(expectedData)188 .expect(422);189 expect(error).toMatchObject(errorMessage);190 });191 });...
find-entity-name.test.js
Source:find-entity-name.test.js
1import findEntityName from './find-entity-name';2/**3 * @status: Complete4 * @sign-off-by: Alex Andries5 */6const mockData = {7 srcip: '10.202.45.175',8 dstip: '23.41.93.69',9 ipv4: 'ipv4',10 user_id: 1,11 user: 'demo',12 domain: 'demo.com',13 source_ip: '123.123.123.0',14 uid: 'demo-uid-test',15};16describe('find-entity-name', () => {17 it('should return entity name for sip', () => {18 const expectedData = '10.202.45.175';19 expect(findEntityName('sip', mockData)).toEqual(expectedData);20 });21 it('should return entity name for sipdip', () => {22 const expectedData = '10.202.45.175 23.41.93.69';23 expect(findEntityName('sipdip', mockData)).toEqual(expectedData);24 });25 it('should return entity name for dip', () => {26 const expectedData = '23.41.93.69';27 expect(findEntityName('dip', mockData)).toEqual(expectedData);28 });29 it('should return entity name for hpa', () => {30 const expectedData = 'ipv4';31 expect(findEntityName('hpa', mockData)).toEqual(expectedData);32 });33 it('should return entity name for user', () => {34 const expectedData = 1;35 expect(findEntityName('user', mockData)).toEqual(expectedData);36 });37 it('should return entity name for useraccess', () => {38 const expectedData = 'demo';39 expect(findEntityName('useraccess', mockData)).toEqual(expectedData);40 });41 it('should return entity name for username', () => {42 const expectedData = 'demo';43 expect(findEntityName('username', mockData)).toEqual(expectedData);44 });45 it('should return entity name for domain', () => {46 const expectedData = 'demo.com';47 expect(findEntityName('domain', mockData)).toEqual(expectedData);48 });49 it('should return entity name for sipdomain', () => {50 const expectedData = '10.202.45.175 demo.com';51 expect(findEntityName('sipdomain', mockData)).toEqual(expectedData);52 });53 it('should return entity name for request', () => {54 const expectedData = '123.123.123.0 demo-uid-test';55 expect(findEntityName('request', mockData)).toEqual(expectedData);56 });...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 wpt.getTestResults(data.data.testId, function(err, data) {5 if (err) return console.error(err);6 console.log(data);7 });8});
Using AI Code Generation
1var wpt = require('webpagetest');2var test = new wpt('www.webpagetest.org');3test.runTest('www.google.com', function(err, data) {4 if (err) {5 console.log('Error: ' + err);6 } else {7 test.expectedData(data.data.testId, function(err, data) {8 if (err) {9 console.log('Error: ' + err);10 } else {11 console.log(data);12 }13 });14 }15});16var wpt = require('webpagetest');17var test = new wpt('www.webpagetest.org');18test.getLocations(function(err, data) {19 if (err) {20 console.log('Error: ' + err);21 } else {22 console.log(data);23 }24});25var wpt = require('webpagetest');26var test = new wpt('www.webpagetest.org');27test.getLocations(function(err, data) {28 if (err) {29 console.log('Error: ' + err);30 } else {31 console.log(data);32 }33});34var wpt = require('webpagetest');35var test = new wpt('www.webpagetest.org');36test.getTesters(function(err, data) {37 if (err) {38 console.log('Error: ' + err);39 } else {40 console.log(data);41 }42});43var wpt = require('webpagetest');44var test = new wpt('www.webpagetest.org');45test.getTesters(function(err, data) {46 if (err) {47 console.log('Error: ' + err);48 } else {49 console.log(data);50 }51});52var wpt = require('webpagetest');53var test = new wpt('www.webpagetest.org');54test.getTesters(function(err, data) {55 if (err) {56 console.log('Error: ' + err);57 } else {58 console.log(data);59 }60});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!