-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdateUser.spec.js
More file actions
135 lines (111 loc) · 4.2 KB
/
updateUser.spec.js
File metadata and controls
135 lines (111 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// @ts-check
import { expect, test } from '@playwright/test';
// Base API URL - adjust this to match your actual API endpoint
const API_BASE_URL = process.env.API_BASE_URL || 'https://dummyjson.com';
const USERS_ENDPOINT = '/users';
const AUTH_ENDPOINT = '/auth/login';
test.describe('PUT / PATCH Update User API', () => {
test('Update user details ',{tag: '@api'}, async ({ request }) => {
const userId = 1;
const updateData = {
firstName: 'John',
lastName: 'Doe',
age: 30
};
// Try PUT first, fallback to PATCH if needed
const response = await request.put(`${API_BASE_URL}${USERS_ENDPOINT}/${userId}`, {
data: updateData
});
expect(response.status()).toBe(200);
const body = await response.json();
expect(body).toHaveProperty('id', userId);
expect(body).toHaveProperty('firstName', updateData.firstName);
expect(body).toHaveProperty('lastName', updateData.lastName);
});
test('Update user with empty payload ',{tag: '@api'}, async ({ request }) => {
const userId = 2;
const response = await request.put(`${API_BASE_URL}${USERS_ENDPOINT}/${userId}`, {
data: {}
});
expect(response.status()).toBe(200);
const body = await response.json();
expect(body).toBeInstanceOf(Object);
expect(body).toHaveProperty('id', userId);
});
test('Update only one field ',{tag: '@api'}, async ({ request }) => {
const userId = 3;
const updateData = {
firstName: 'UpdatedFirstName'
};
// Use PATCH for partial update
const response = await request.patch(`${API_BASE_URL}${USERS_ENDPOINT}/${userId}`, {
data: updateData
});
expect(response.status()).toBe(200);
const body = await response.json();
expect(body).toHaveProperty('id', userId);
expect(body).toHaveProperty('firstName', updateData.firstName);
});
test('Validate returned name field ',{tag: '@api'}, async ({ request }) => {
const userId = 4;
const updateData = {
firstName: 'Jane',
lastName: 'Smith'
};
const response = await request.put(`${API_BASE_URL}${USERS_ENDPOINT}/${userId}`, {
data: updateData
});
expect(response.status()).toBe(200);
const body = await response.json();
expect(body).toHaveProperty('firstName');
expect(body).toHaveProperty('lastName');
expect(typeof body.firstName).toBe('string');
expect(typeof body.lastName).toBe('string');
expect(body.firstName).toBe(updateData.firstName);
expect(body.lastName).toBe(updateData.lastName);
});
test('Update and validate response contains updatedAt simulation ',{tag: '@api'}, async ({ request }) => {
const userId = 5;
const updateData = {
firstName: 'Updated',
lastName: 'User'
};
const response = await request.put(`${API_BASE_URL}${USERS_ENDPOINT}/${userId}`, {
data: updateData
});
expect(response.status()).toBe(200);
const body = await response.json();
// Check for updatedAt or similar timestamp field
const hasTimestamp = body.hasOwnProperty('updatedAt') ||
body.hasOwnProperty('updated') ||
body.hasOwnProperty('modifiedAt');
// At minimum, validate the response structure
expect(body).toBeInstanceOf(Object);
expect(body).toHaveProperty('id', userId);
});
test('Login failure (invalid creds) ',{tag: '@api'}, async ({ request }) => {
const loginData = {
username: 'invaliduser',
password: 'wrongpassword'
};
const response = await request.post(`${API_BASE_URL}${AUTH_ENDPOINT}`, {
data: loginData
});
// Should return error status (400 or 401)
expect([400, 401, 403]).toContain(response.status());
const body = await response.json();
expect(body).toBeInstanceOf(Object);
});
test('Login missing fields returns 400 ',{tag: '@api'}, async ({ request }) => {
// Missing password field
const loginData = {
username: 'kminchelle'
};
const response = await request.post(`${API_BASE_URL}${AUTH_ENDPOINT}`, {
data: loginData
});
expect(response.status()).toBe(400);
const body = await response.json();
expect(body).toBeInstanceOf(Object);
});
});