@@ -9,55 +9,51 @@ function generateJwt(user = { id: 'u1', role: 'user' }) {
99}
1010
1111describe ( 'Integration: User, Product, Blog Endpoints' , ( ) => {
12- let userToken , adminToken , createdProductId ;
12+ let userToken , adminToken , createdProductId ;
1313 beforeAll ( ( ) => {
14- userToken = generateJwt ( { id : 'u1' , role : 'user' } ) ;
15- adminToken = generateJwt ( { id : 'admin1' , role : 'admin' } ) ;
14+ userToken = generateJwt ( { id : 'u1' , role : 'user' } ) ;
15+ adminToken = generateJwt ( { id : 'admin1' , role : 'admin' } ) ;
1616 } ) ;
1717
1818 it ( 'should register a new user' , async ( ) => {
19- const res = await request ( app )
20- . post ( '/auth/register' )
21- . send ( {
22- username : 'integrationUser' ,
23- email : 'int@example.com' ,
24- password : 'pass123' ,
25- firstName : 'Integration' ,
26- lastName : 'User' ,
27- role : 'user'
28- } ) ;
29- expect ( [ 200 , 201 ] ) . toContain ( res . statusCode ) ;
19+ const res = await request ( app ) . post ( '/auth/register' ) . send ( {
20+ username : 'integrationUser' ,
21+ email : 'int@example.com' ,
22+ password : 'pass123' ,
23+ firstName : 'Integration' ,
24+ lastName : 'User' ,
25+ role : 'user' ,
26+ } ) ;
27+ expect ( [ 200 , 201 ] ) . toContain ( res . statusCode ) ;
3028 expect ( res . body ) . toHaveProperty ( 'data' ) ;
3129 } ) ;
3230
3331 it ( 'should create a product (protected)' , async ( ) => {
3432 const res = await request ( app )
3533 . post ( '/products' )
36- . set ( 'Authorization' , `Bearer ${ userToken } ` )
37- . send ( {
38- name : 'Integration Product' ,
39- price : 10 ,
40- description : 'A product for integration testing' ,
41- category : 'test' ,
42- createdBy : 'u1'
43- } ) ;
44- expect ( [ 200 , 201 , 400 ] ) . toContain ( res . statusCode ) ;
45- if ( res . body . data && res . body . data . createdProduct && res . body . data . createdProduct . id ) {
46- createdProductId = res . body . data . createdProduct . id ;
47- }
34+ . set ( 'Authorization' , `Bearer ${ userToken } ` )
35+ . send ( {
36+ name : 'Integration Product' ,
37+ price : 10 ,
38+ description : 'A product for integration testing' ,
39+ category : 'test' ,
40+ createdBy : 'u1' ,
41+ } ) ;
42+ expect ( [ 200 , 201 , 400 ] ) . toContain ( res . statusCode ) ;
43+ if ( res . body . data && res . body . data . createdProduct && res . body . data . createdProduct . id ) {
44+ createdProductId = res . body . data . createdProduct . id ;
45+ }
4846 } ) ;
4947
50- it ( 'should not create a product without auth' , async ( ) => {
51- const res = await request ( app )
52- . post ( '/products' )
53- . send ( {
54- name : 'NoAuth Product' ,
55- price : 10 ,
56- description : 'No auth' ,
57- category : 'test' ,
58- createdBy : 'u1'
59- } ) ;
60- expect ( [ 401 , 403 ] ) . toContain ( res . statusCode ) ;
48+ it ( 'should not create a product without auth' , async ( ) => {
49+ const res = await request ( app ) . post ( '/products' ) . send ( {
50+ name : 'NoAuth Product' ,
51+ price : 10 ,
52+ description : 'No auth' ,
53+ category : 'test' ,
54+ createdBy : 'u1' ,
55+ } ) ;
56+ expect ( [ 401 , 403 ] ) . toContain ( res . statusCode ) ;
6157 } ) ;
6258
6359 it ( 'should get all products (public)' , async ( ) => {
@@ -66,60 +62,55 @@ describe('Integration: User, Product, Blog Endpoints', () => {
6662 expect ( Array . isArray ( res . body . data ?. products || res . body . data ) ) . toBe ( true ) ;
6763 } ) ;
6864
69- it ( 'should update a product (protected)' , async ( ) => {
70- if ( ! createdProductId ) return ;
71- const res = await request ( app )
72- . put ( `/products/${ createdProductId } ` )
73- . set ( 'Authorization' , `Bearer ${ userToken } ` )
74- . send ( {
75- name : 'Updated Product' ,
76- price : 15 ,
77- description : 'Updated description' ,
78- category : 'test' ,
79- createdBy : 'u1'
80- } ) ;
81- expect ( [ 200 , 201 , 400 , 404 ] ) . toContain ( res . statusCode ) ;
82- } ) ;
65+ it ( 'should update a product (protected)' , async ( ) => {
66+ if ( ! createdProductId ) return ;
67+ const res = await request ( app )
68+ . put ( `/products/${ createdProductId } ` )
69+ . set ( 'Authorization' , `Bearer ${ userToken } ` )
70+ . send ( {
71+ name : 'Updated Product' ,
72+ price : 15 ,
73+ description : 'Updated description' ,
74+ category : 'test' ,
75+ createdBy : 'u1' ,
76+ } ) ;
77+ expect ( [ 200 , 201 , 400 , 404 ] ) . toContain ( res . statusCode ) ;
78+ } ) ;
8379
84- it ( 'should not update a product without auth' , async ( ) => {
85- if ( ! createdProductId ) return ;
86- const res = await request ( app )
87- . put ( `/products/${ createdProductId } ` )
88- . send ( {
89- name : 'Updated Product' ,
90- price : 15 ,
91- description : 'Updated description' ,
92- category : 'test' ,
93- createdBy : 'u1'
94- } ) ;
95- expect ( [ 401 , 403 ] ) . toContain ( res . statusCode ) ;
80+ it ( 'should not update a product without auth' , async ( ) => {
81+ if ( ! createdProductId ) return ;
82+ const res = await request ( app ) . put ( `/products/${ createdProductId } ` ) . send ( {
83+ name : 'Updated Product' ,
84+ price : 15 ,
85+ description : 'Updated description' ,
86+ category : 'test' ,
87+ createdBy : 'u1' ,
9688 } ) ;
89+ expect ( [ 401 , 403 ] ) . toContain ( res . statusCode ) ;
90+ } ) ;
9791
98- it ( 'should delete a product as admin' , async ( ) => {
99- if ( ! createdProductId ) return ;
100- const res = await request ( app )
101- . delete ( `/products/${ createdProductId } ` )
102- . set ( 'Authorization' , `Bearer ${ adminToken } ` ) ;
103- expect ( [ 200 , 201 , 404 ] ) . toContain ( res . statusCode ) ;
104- } ) ;
92+ it ( 'should delete a product as admin' , async ( ) => {
93+ if ( ! createdProductId ) return ;
94+ const res = await request ( app )
95+ . delete ( `/products/${ createdProductId } ` )
96+ . set ( 'Authorization' , `Bearer ${ adminToken } ` ) ;
97+ expect ( [ 200 , 201 , 404 ] ) . toContain ( res . statusCode ) ;
98+ } ) ;
10599
106- it ( 'should not delete a product as user' , async ( ) => {
107- if ( ! createdProductId ) return ;
108- const res = await request ( app )
109- . delete ( `/products/${ createdProductId } ` )
110- . set ( 'Authorization' , `Bearer ${ userToken } ` ) ;
111- expect ( [ 401 , 403 ] ) . toContain ( res . statusCode ) ;
112- } ) ;
100+ it ( 'should not delete a product as user' , async ( ) => {
101+ if ( ! createdProductId ) return ;
102+ const res = await request ( app )
103+ . delete ( `/products/${ createdProductId } ` )
104+ . set ( 'Authorization' , `Bearer ${ userToken } ` ) ;
105+ expect ( [ 401 , 403 ] ) . toContain ( res . statusCode ) ;
106+ } ) ;
113107
114108 it ( 'should create a blog (protected)' , async ( ) => {
115- const res = await request ( app )
116- . post ( '/blogs' )
117- . set ( 'Authorization' , `Bearer ${ userToken } ` )
118- . send ( {
119- title : 'Integration Blog' ,
120- content : 'Lorem ipsum' ,
121- author : 'u1'
122- } ) ;
109+ const res = await request ( app ) . post ( '/blogs' ) . set ( 'Authorization' , `Bearer ${ userToken } ` ) . send ( {
110+ title : 'Integration Blog' ,
111+ content : 'Lorem ipsum' ,
112+ author : 'u1' ,
113+ } ) ;
123114 expect ( [ 200 , 201 , 400 ] ) . toContain ( res . statusCode ) ;
124115 } ) ;
125116
@@ -129,5 +120,5 @@ describe('Integration: User, Product, Blog Endpoints', () => {
129120 expect ( Array . isArray ( res . body . data ?. blogs || res . body . data ) ) . toBe ( true ) ;
130121 } ) ;
131122
132- // Add more blog update/delete tests if implemented
123+ // Add more blog update/delete tests if implemented
133124} ) ;
0 commit comments