@@ -3,6 +3,7 @@ const sinon = require("sinon");
3
3
const { expect } = chai ;
4
4
const chaiHttp = require ( "chai-http" ) ;
5
5
6
+ const firestore = require ( "../../utils/firestore" ) ;
6
7
const app = require ( "../../server" ) ;
7
8
const tasks = require ( "../../models/tasks" ) ;
8
9
const authService = require ( "../../services/authService" ) ;
@@ -958,19 +959,7 @@ describe("Tasks", function () {
958
959
expect ( res . body . message ) . to . be . equal ( "Status cannot be updated. Please contact admin." ) ;
959
960
} ) ;
960
961
961
- it ( "Should give 400 if percentCompleted is not 100 and new status is COMPLETED " , async function ( ) {
962
- taskId = ( await tasks . updateTask ( { ...taskData , status : "REVIEW" , assignee : appOwner . username } ) ) . taskId ;
963
- const res = await chai
964
- . request ( app )
965
- . patch ( `/tasks/self/${ taskId } ` )
966
- . set ( "cookie" , `${ cookieName } =${ jwt } ` )
967
- . send ( { ...taskStatusData , status : "COMPLETED" } ) ;
968
-
969
- expect ( res ) . to . have . status ( 400 ) ;
970
- expect ( res . body . message ) . to . be . equal ( "Status cannot be updated. Task is not completed yet" ) ;
971
- } ) ;
972
-
973
- it ( "Should give 400 if percentCompleted is not 100 and new status is DONE under feature flag " , async function ( ) {
962
+ it ( "Should give 400 if percentCompleted is not 100 and new status is DONE" , async function ( ) {
974
963
taskId = ( await tasks . updateTask ( { ...taskData , status : "REVIEW" , assignee : appOwner . username } ) ) . taskId ;
975
964
const res = await chai
976
965
. request ( app )
@@ -982,19 +971,7 @@ describe("Tasks", function () {
982
971
expect ( res . body . message ) . to . be . equal ( "Status cannot be updated. Task is not done yet" ) ;
983
972
} ) ;
984
973
985
- it ( "Should give 400 if percentCompleted is not 100 and new status is VERIFIED " , async function ( ) {
986
- taskId = ( await tasks . updateTask ( { ...taskData , status : "REVIEW" , assignee : appOwner . username } ) ) . taskId ;
987
- const res = await chai
988
- . request ( app )
989
- . patch ( `/tasks/self/${ taskId } ` )
990
- . set ( "cookie" , `${ cookieName } =${ jwt } ` )
991
- . send ( { ...taskStatusData , status : "VERIFIED" } ) ;
992
-
993
- expect ( res ) . to . have . status ( 400 ) ;
994
- expect ( res . body . message ) . to . be . equal ( "Status cannot be updated. Task is not completed yet" ) ;
995
- } ) ;
996
-
997
- it ( "Should give 400 if percentCompleted is not 100 and new status is VERIFIED under feature flag" , async function ( ) {
974
+ it ( "Should give 400 if percentCompleted is not 100 and new status is VERIFIED" , async function ( ) {
998
975
taskId = ( await tasks . updateTask ( { ...taskData , status : "REVIEW" , assignee : appOwner . username } ) ) . taskId ;
999
976
const res = await chai
1000
977
. request ( app )
@@ -1006,32 +983,7 @@ describe("Tasks", function () {
1006
983
expect ( res . body . message ) . to . be . equal ( "Status cannot be updated. Task is not done yet" ) ;
1007
984
} ) ;
1008
985
1009
- it ( "Should give 400 if status is COMPLETED and newpercent is less than 100" , async function ( ) {
1010
- const taskData = {
1011
- title : "Test task" ,
1012
- type : "feature" ,
1013
- endsOn : 1234 ,
1014
- startedOn : 4567 ,
1015
- status : "COMPLETED" ,
1016
- percentCompleted : 100 ,
1017
- participants : [ ] ,
1018
- assignee : appOwner . username ,
1019
- completionAward : { [ DINERO ] : 3 , [ NEELAM ] : 300 } ,
1020
- lossRate : { [ DINERO ] : 1 } ,
1021
- isNoteworthy : true ,
1022
- } ;
1023
- taskId = ( await tasks . updateTask ( taskData ) ) . taskId ;
1024
- const res = await chai
1025
- . request ( app )
1026
- . patch ( `/tasks/self/${ taskId } ` )
1027
- . set ( "cookie" , `${ cookieName } =${ jwt } ` )
1028
- . send ( { percentCompleted : 80 } ) ;
1029
-
1030
- expect ( res ) . to . have . status ( 400 ) ;
1031
- expect ( res . body . message ) . to . be . equal ( "Task percentCompleted can't updated as status is COMPLETED" ) ;
1032
- } ) ;
1033
-
1034
- it ( "Should give 400 if status is DONE and newpercent is less than 100 under feature flag" , async function ( ) {
986
+ it ( "Should give 400 if status is DONE and newpercent is less than 100" , async function ( ) {
1035
987
taskId = ( await tasks . updateTask ( updateTaskStatus [ 0 ] ) ) . taskId ;
1036
988
const res = await chai
1037
989
. request ( app )
@@ -1070,4 +1022,51 @@ describe("Tasks", function () {
1070
1022
expect ( res . body . message ) . to . be . equal ( "No overdue tasks found" ) ;
1071
1023
} ) ;
1072
1024
} ) ;
1025
+
1026
+ describe ( "POST /tasks/migration" , function ( ) {
1027
+ it ( "Should update status COMPLETED to DONE successful" , async function ( ) {
1028
+ const taskData1 = { status : "COMPLETED" } ;
1029
+ await firestore . collection ( "tasks" ) . doc ( "updateTaskStatus1" ) . set ( taskData1 ) ;
1030
+ const res = await chai . request ( app ) . post ( "/tasks/migration" ) . set ( "cookie" , `${ cookieName } =${ superUserJwt } ` ) ;
1031
+ expect ( res ) . to . have . status ( 200 ) ;
1032
+ expect ( res . body . totalTasks ) . to . be . equal ( 1 ) ;
1033
+ expect ( res . body . totalUpdatedStatus ) . to . be . equal ( 1 ) ;
1034
+ expect ( res . body . updatedTaskDetails ) . to . deep . equal ( [ "updateTaskStatus1" ] ) ;
1035
+ expect ( res . body . totalOperationsFailed ) . to . be . equal ( 0 ) ;
1036
+ expect ( res . body . failedTaskDetails ) . to . deep . equal ( [ ] ) ;
1037
+ } ) ;
1038
+
1039
+ it ( "Should not update if not found any COMPLETED task status " , async function ( ) {
1040
+ const res = await chai . request ( app ) . post ( "/tasks/migration" ) . set ( "cookie" , `${ cookieName } =${ superUserJwt } ` ) ;
1041
+ expect ( res ) . to . have . status ( 200 ) ;
1042
+ expect ( res . body . totalTasks ) . to . be . equal ( 0 ) ;
1043
+ expect ( res . body . totalUpdatedStatus ) . to . be . equal ( 0 ) ;
1044
+ expect ( res . body . updatedTaskDetails ) . to . deep . equal ( [ ] ) ;
1045
+ expect ( res . body . totalOperationsFailed ) . to . be . equal ( 0 ) ;
1046
+ expect ( res . body . failedTaskDetails ) . to . deep . equal ( [ ] ) ;
1047
+ } ) ;
1048
+
1049
+ it ( "should throw an error if firestore batch operations fail" , async function ( ) {
1050
+ const stub = sinon . stub ( firestore , "batch" ) ;
1051
+ stub . returns ( {
1052
+ update : function ( ) { } ,
1053
+ commit : function ( ) {
1054
+ throw new Error ( "Firestore batch commit failed!" ) ;
1055
+ } ,
1056
+ } ) ;
1057
+ const taskData1 = { status : "COMPLETED" } ;
1058
+ await firestore . collection ( "tasks" ) . doc ( "updateTaskStatus1" ) . set ( taskData1 ) ;
1059
+ const res = await chai . request ( app ) . post ( "/tasks/migration" ) . set ( "cookie" , `${ cookieName } =${ superUserJwt } ` ) ;
1060
+ expect ( res . status ) . to . equal ( 500 ) ;
1061
+ const response = res . body ;
1062
+ expect ( response . message ) . to . be . equal ( "An internal server error occurred" ) ;
1063
+ } ) ;
1064
+
1065
+ it ( "Should return 401 if not super_user" , async function ( ) {
1066
+ const nonSuperUserId = await addUser ( appOwner ) ;
1067
+ const nonSuperUserJwt = authService . generateAuthToken ( { userId : nonSuperUserId } ) ;
1068
+ const res = await chai . request ( app ) . post ( "/tasks/migration" ) . set ( "cookie" , `${ cookieName } =${ nonSuperUserJwt } ` ) ;
1069
+ expect ( res ) . to . have . status ( 401 ) ;
1070
+ } ) ;
1071
+ } ) ;
1073
1072
} ) ;
0 commit comments