@@ -28,9 +28,15 @@ const { logType } = require("../../constants/logs");
28
28
const { INTERNAL_SERVER_ERROR } = require ( "../../constants/errorMessages" ) ;
29
29
const tasksService = require ( "../../services/tasks" ) ;
30
30
chai . use ( chaiHttp ) ;
31
+ const tags = require ( "../../models/tags" ) ;
32
+ const levels = require ( "../../models/levels" ) ;
33
+ const items = require ( "../../models/items" ) ;
34
+ const taskController = require ( "../../controllers/tasks" ) ;
31
35
32
36
const appOwner = userData [ 3 ] ;
33
37
const superUser = userData [ 4 ] ;
38
+ const genZUser = userData [ 20 ] ;
39
+ const testUser = userData [ 2 ] ;
34
40
35
41
let jwt , superUserJwt ;
36
42
const { createProgressDocument } = require ( "../../models/progresses" ) ;
@@ -77,14 +83,40 @@ const taskData = [
77
83
} ,
78
84
] ;
79
85
86
+ const tagData = {
87
+ reason : "adding skills to users" ,
88
+ name : "EMBER" ,
89
+ type : "SKILL" ,
90
+ createdBy : "" ,
91
+ date : new Date ( ) . getTime ( ) ,
92
+ } ;
93
+
94
+ const itemData = {
95
+ itemId : "" ,
96
+ itemType : "TASK" ,
97
+ tagPayload : [
98
+ {
99
+ tagId : "" ,
100
+ levelId : "" ,
101
+ } ,
102
+ ] ,
103
+ } ;
104
+
105
+ const levelData = {
106
+ name : "1" ,
107
+ value : 1 ,
108
+ } ;
109
+
80
110
describe ( "Tasks" , function ( ) {
81
- let taskId1 , taskId ;
111
+ let taskId1 , taskId , testUserId , testUserjwt ;
82
112
83
113
before ( async function ( ) {
84
114
const userId = await addUser ( appOwner ) ;
85
115
const superUserId = await addUser ( superUser ) ;
116
+ testUserId = await addUser ( testUser ) ;
86
117
jwt = authService . generateAuthToken ( { userId } ) ;
87
118
superUserJwt = authService . generateAuthToken ( { userId : superUserId } ) ;
119
+ testUserjwt = authService . generateAuthToken ( { userId : testUserId } ) ;
88
120
89
121
// Add the active task
90
122
taskId = ( await tasks . updateTask ( taskData [ 0 ] ) ) . taskId ;
@@ -1699,4 +1731,81 @@ describe("Tasks", function () {
1699
1731
expect ( res . body . message ) . to . be . equal ( INTERNAL_SERVER_ERROR ) ;
1700
1732
} ) ;
1701
1733
} ) ;
1734
+
1735
+ describe ( "PATCH /tasks/assign/:userId" , function ( ) {
1736
+ let taskData , genZUserJwt , genZUserId ;
1737
+
1738
+ beforeEach ( async function ( ) {
1739
+ genZUserId = await addUser ( genZUser ) ;
1740
+ genZUserJwt = authService . generateAuthToken ( { userId : genZUserId } ) ;
1741
+ taskData = tasksData [ 8 ] ;
1742
+ } ) ;
1743
+
1744
+ afterEach ( async function ( ) {
1745
+ await cleanDb ( ) ;
1746
+ sinon . restore ( ) ;
1747
+ } ) ;
1748
+
1749
+ it ( "Should not assign a task to the user if they do not have status idle" , async function ( ) {
1750
+ await tasks . updateTask ( taskData ) ;
1751
+
1752
+ const res = await chai
1753
+ . request ( app )
1754
+ . patch ( `/tasks/assign/${ testUserId } ?dev=true` )
1755
+ . set ( "cookie" , `${ cookieName } =${ testUserjwt } ` )
1756
+ . send ( ) ;
1757
+
1758
+ expect ( res ) . to . have . status ( 200 ) ;
1759
+ expect ( res . body . message ) . to . be . equal ( "Task cannot be assigned to users with active or OOO status" ) ;
1760
+ } ) ;
1761
+
1762
+ it ( "Should not assign a task to the user if task doesn't exist" , async function ( ) {
1763
+ await tasks . updateTask ( taskData ) ;
1764
+
1765
+ const res = await chai
1766
+ . request ( app )
1767
+ . patch ( `/tasks/assign/${ genZUserId } ?dev=true` )
1768
+ . set ( "cookie" , `${ cookieName } =${ genZUserJwt } ` )
1769
+ . send ( ) ;
1770
+
1771
+ expect ( res ) . to . have . status ( 200 ) ;
1772
+ expect ( res . body . message ) . to . be . equal ( "Task not found" ) ;
1773
+ } ) ;
1774
+
1775
+ it ( "Should assign task to the user if their status is idle and task is available" , async function ( ) {
1776
+ const taskAdd = await tasks . updateTask ( taskData ) ;
1777
+ const levelAdd = await levels . addLevel ( levelData ) ;
1778
+
1779
+ tagData . createdBy = genZUserId ;
1780
+ const tagAdd = await tags . addTag ( tagData ) ;
1781
+
1782
+ itemData . itemId = taskAdd . taskId ;
1783
+ itemData . tagPayload [ 0 ] . tagId = tagAdd . id ;
1784
+ itemData . tagPayload [ 0 ] . levelId = levelAdd . id ;
1785
+
1786
+ await items . addTagsToItem ( itemData ) ;
1787
+
1788
+ const res = await chai
1789
+ . request ( app )
1790
+ . patch ( `/tasks/assign/${ genZUserId } ?dev=true` )
1791
+ . set ( "cookie" , `${ cookieName } =${ genZUserJwt } ` )
1792
+ . send ( ) ;
1793
+
1794
+ expect ( res ) . to . have . status ( 200 ) ;
1795
+ expect ( res . body . message ) . to . be . equal ( "Task assigned" ) ;
1796
+ } ) ;
1797
+
1798
+ it ( "Should throw an error if Firestore batch operations fail" , async function ( ) {
1799
+ sinon . stub ( taskController , "assignTask" ) . rejects ( new Error ( INTERNAL_SERVER_ERROR ) ) ;
1800
+
1801
+ const res = await chai
1802
+ . request ( app )
1803
+ . patch ( `/tasks/assign/${ genZUserId } ?dev=true` )
1804
+ . set ( "cookie" , `${ cookieName } =${ genZUserJwt } ` )
1805
+ . send ( ) ;
1806
+
1807
+ expect ( res ) . to . have . status ( 500 ) ;
1808
+ expect ( res . body . message ) . to . be . equal ( INTERNAL_SERVER_ERROR ) ;
1809
+ } ) ;
1810
+ } ) ;
1702
1811
} ) ;
0 commit comments