@@ -978,6 +978,159 @@ def test_restore_entry_notify(self, mock_task):
978
978
979
979
self .assertTrue (mock_task .called )
980
980
981
+ @mock .patch ("entry.tasks.copy_entry.delay" , mock .Mock (side_effect = tasks .copy_entry ))
982
+ def test_copy_entry (self ):
983
+ entry : Entry = self .add_entry (self .user , "entry" , self .entity )
984
+ params = {"copy_entry_names" : ["copy1" , "copy2" ]}
985
+
986
+ resp = self .client .post (
987
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
988
+ )
989
+ self .assertEqual (resp .status_code , 200 )
990
+ self .assertTrue (
991
+ Entry .objects .filter (name = "copy1" , schema = self .entity , is_active = True ).exists ()
992
+ )
993
+ self .assertTrue (
994
+ Entry .objects .filter (name = "copy2" , schema = self .entity , is_active = True ).exists ()
995
+ )
996
+
997
+ def test_copy_entry_without_permission (self ):
998
+ entry : Entry = self .add_entry (self .user , "entry" , self .entity )
999
+ params = {"copy_entry_names" : ["copy1" ]}
1000
+
1001
+ # permission nothing entity
1002
+ self .entity .is_public = False
1003
+ self .entity .save ()
1004
+ resp = self .client .post (
1005
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1006
+ )
1007
+ self .assertEqual (resp .status_code , 403 )
1008
+ self .assertEqual (
1009
+ resp .json (), {"detail" : "You do not have permission to perform this action." }
1010
+ )
1011
+
1012
+ # permission readable entity
1013
+ self .role .users .add (self .user )
1014
+ self .role .permissions .add (self .entity .readable )
1015
+ resp = self .client .post (
1016
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1017
+ )
1018
+ self .assertEqual (resp .status_code , 403 )
1019
+ self .assertEqual (
1020
+ resp .json (), {"detail" : "You do not have permission to perform this action." }
1021
+ )
1022
+
1023
+ # permission writable entity
1024
+ self .role .permissions .add (self .entity .writable )
1025
+ resp = self .client .post (
1026
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1027
+ )
1028
+ self .assertEqual (
1029
+ resp .json (), {"detail" : "You do not have permission to perform this action." }
1030
+ )
1031
+
1032
+ # permission full entity
1033
+ self .role .permissions .add (self .entity .full )
1034
+ resp = self .client .post (
1035
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1036
+ )
1037
+ self .assertEqual (resp .status_code , 200 )
1038
+
1039
+ params = {"copy_entry_names" : ["copy2" ]}
1040
+
1041
+ # permission nothing entry
1042
+ entry .is_public = False
1043
+ entry .save ()
1044
+ resp = self .client .post (
1045
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1046
+ )
1047
+ self .assertEqual (resp .status_code , 403 )
1048
+ self .assertEqual (
1049
+ resp .json (), {"detail" : "You do not have permission to perform this action." }
1050
+ )
1051
+
1052
+ # permission readable entry
1053
+ self .role .permissions .add (entry .readable )
1054
+ resp = self .client .post (
1055
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1056
+ )
1057
+ self .assertEqual (resp .status_code , 403 )
1058
+ self .assertEqual (
1059
+ resp .json (), {"detail" : "You do not have permission to perform this action." }
1060
+ )
1061
+
1062
+ # permission writable entry
1063
+ self .role .permissions .add (entry .writable )
1064
+ resp = self .client .post (
1065
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1066
+ )
1067
+ self .assertEqual (resp .status_code , 403 )
1068
+ self .assertEqual (
1069
+ resp .json (), {"detail" : "You do not have permission to perform this action." }
1070
+ )
1071
+
1072
+ # permission full entry
1073
+ self .role .permissions .add (entry .full )
1074
+ resp = self .client .post (
1075
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1076
+ )
1077
+ self .assertEqual (resp .status_code , 200 )
1078
+
1079
+ def test_copy_entry_with_invalid_param (self ):
1080
+ params = {"copy_entry_names" : ["copy1" ]}
1081
+
1082
+ resp = self .client .post (
1083
+ "/entry/api/v2/%s/copy/" % "hoge" , json .dumps (params ), "application/json"
1084
+ )
1085
+ self .assertEqual (resp .status_code , 404 )
1086
+
1087
+ resp = self .client .post (
1088
+ "/entry/api/v2/%s/copy/" % 9999 , json .dumps (params ), "application/json"
1089
+ )
1090
+ self .assertEqual (resp .status_code , 404 )
1091
+ self .assertEqual (resp .json (), {"detail" : "Not found." })
1092
+
1093
+ entry = self .add_entry (self .user , "entry" , self .entity )
1094
+
1095
+ params = {}
1096
+ resp = self .client .post (
1097
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1098
+ )
1099
+ self .assertEqual (resp .status_code , 400 )
1100
+ self .assertEqual (resp .json (), {"copy_entry_names" : ["This field is required." ]})
1101
+
1102
+ params = {"copy_entry_names" : "hoge" }
1103
+ resp = self .client .post (
1104
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1105
+ )
1106
+ self .assertEqual (resp .status_code , 400 )
1107
+ self .assertEqual (
1108
+ resp .json (), {"copy_entry_names" : ['Expected a list of items but got type "str".' ]}
1109
+ )
1110
+
1111
+ params = {"copy_entry_names" : [{}]}
1112
+ resp = self .client .post (
1113
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1114
+ )
1115
+ self .assertEqual (resp .status_code , 400 )
1116
+ self .assertEqual (resp .json (), {"copy_entry_names" : {"0" : ["Not a valid string." ]}})
1117
+
1118
+ params = {"copy_entry_names" : []}
1119
+ resp = self .client .post (
1120
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1121
+ )
1122
+ self .assertEqual (resp .status_code , 400 )
1123
+ self .assertEqual (resp .json (), {"copy_entry_names" : ["This list may not be empty." ]})
1124
+
1125
+ params = {"copy_entry_names" : ["entry" ]}
1126
+ resp = self .client .post (
1127
+ "/entry/api/v2/%s/copy/" % entry .id , json .dumps (params ), "application/json"
1128
+ )
1129
+ self .assertEqual (resp .status_code , 400 )
1130
+ self .assertEqual (
1131
+ resp .json (), {"copy_entry_names" : ["specified name(entry) already exists" ]}
1132
+ )
1133
+
981
1134
def test_serach_entry (self ):
982
1135
ref_entry4 = self .add_entry (self .user , "hoge4" , self .ref_entity )
983
1136
ref_entry5 = self .add_entry (self .user , "hoge5" , self .ref_entity )
0 commit comments