Mercurial > p > roundup > code
comparison test/rest_common.py @ 5711:aea2cc142c1b
Added some more rest testing and make sure api version is valid.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 15 Apr 2019 21:41:06 -0400 |
| parents | 0b79bfcb3312 |
| children | e199d0ae4a25 |
comparison
equal
deleted
inserted
replaced
| 5710:0b79bfcb3312 | 5711:aea2cc142c1b |
|---|---|
| 952 del(self.headers) | 952 del(self.headers) |
| 953 | 953 |
| 954 # TEST #5 | 954 # TEST #5 |
| 955 # POST: create new issue | 955 # POST: create new issue |
| 956 # no etag needed | 956 # no etag needed |
| 957 # FIXME at some point we probably want to implement | |
| 958 # Post Once Only, so we need to add a Post Once Exactly | |
| 959 # test and a resubmit as well. | |
| 960 etag = "not needed" | 957 etag = "not needed" |
| 961 body=b'{ "title": "foo bar", "priority": "critical" }' | 958 body=b'{ "title": "foo bar", "priority": "critical" }' |
| 962 env = { "CONTENT_TYPE": "application/json", | 959 env = { "CONTENT_TYPE": "application/json", |
| 963 "CONTENT_LENGTH": len(body), | 960 "CONTENT_LENGTH": len(body), |
| 964 "REQUEST_METHOD": "POST" | 961 "REQUEST_METHOD": "POST" |
| 984 str(issue_id), # must be a string not unicode | 981 str(issue_id), # must be a string not unicode |
| 985 self.empty_form) | 982 self.empty_form) |
| 986 self.assertEqual(self.dummy_client.response_code, 200) | 983 self.assertEqual(self.dummy_client.response_code, 200) |
| 987 self.assertEqual(results['data']['attributes']['title'], | 984 self.assertEqual(results['data']['attributes']['title'], |
| 988 'foo bar') | 985 'foo bar') |
| 989 del(self.headers) | 986 |
| 990 | 987 # TEST #6 |
| 991 def testPostPOE(self): | 988 # POST: an invalid class |
| 992 ''' test post once exactly: get POE url, create issue | 989 # no etag needed |
| 993 using POE url. Use dispatch entry point. | |
| 994 ''' | |
| 995 import time | |
| 996 # setup environment | |
| 997 etag = "not needed" | 990 etag = "not needed" |
| 998 body=b'{ "title": "foo bar", "priority": "critical" }' | 991 body=b'{ "title": "foo bar", "priority": "critical" }' |
| 999 env = { "CONTENT_TYPE": "application/json", | 992 env = { "CONTENT_TYPE": "application/json", |
| 1000 "CONTENT_LENGTH": len(body), | 993 "CONTENT_LENGTH": len(body), |
| 1001 "REQUEST_METHOD": "POST" | 994 "REQUEST_METHOD": "POST" |
| 1007 self.headers=headers | 1000 self.headers=headers |
| 1008 body_file=BytesIO(body) # FieldStorage needs a file | 1001 body_file=BytesIO(body) # FieldStorage needs a file |
| 1009 form = client.BinaryFieldStorage(body_file, | 1002 form = client.BinaryFieldStorage(body_file, |
| 1010 headers=headers, | 1003 headers=headers, |
| 1011 environ=env) | 1004 environ=env) |
| 1005 self.server.client.request.headers.get=self.get_header | |
| 1006 results = self.server.dispatch('POST', | |
| 1007 "/rest/data/nonissue", | |
| 1008 form) | |
| 1009 | |
| 1010 self.assertEqual(self.server.client.response_code, 404) | |
| 1011 json_dict = json.loads(b2s(results)) | |
| 1012 status=json_dict['error']['status'] | |
| 1013 msg=json_dict['error']['msg'] | |
| 1014 self.assertEqual(status, 404) | |
| 1015 self.assertEqual(msg, 'Class nonissue not found') | |
| 1016 | |
| 1017 # TEST #7 | |
| 1018 # POST: status without key field of name | |
| 1019 # also test that version spec in accept header is accepted | |
| 1020 # no etag needed | |
| 1021 etag = "not needed" | |
| 1022 body=b'{ "order": 5 }' | |
| 1023 env = { "CONTENT_TYPE": "application/json", | |
| 1024 "CONTENT_LENGTH": len(body), | |
| 1025 "REQUEST_METHOD": "POST" | |
| 1026 } | |
| 1027 headers={"accept": "application/json; version=1", | |
| 1028 "content-type": env['CONTENT_TYPE'], | |
| 1029 "content-length": len(body) | |
| 1030 } | |
| 1031 self.headers=headers | |
| 1032 body_file=BytesIO(body) # FieldStorage needs a file | |
| 1033 form = client.BinaryFieldStorage(body_file, | |
| 1034 headers=headers, | |
| 1035 environ=env) | |
| 1036 self.server.client.request.headers.get=self.get_header | |
| 1037 self.db.setCurrentUser('admin') # must be admin to create status | |
| 1038 results = self.server.dispatch('POST', | |
| 1039 "/rest/data/status", | |
| 1040 form) | |
| 1041 | |
| 1042 self.assertEqual(self.server.client.response_code, 400) | |
| 1043 json_dict = json.loads(b2s(results)) | |
| 1044 status=json_dict['error']['status'] | |
| 1045 msg=json_dict['error']['msg'] | |
| 1046 self.assertEqual(status, 400) | |
| 1047 self.assertEqual(msg, "Must provide the 'name' property.") | |
| 1048 | |
| 1049 | |
| 1050 # TEST #8 | |
| 1051 # DELETE: delete issue 1 | |
| 1052 etag = calculate_etag(self.db.issue.getnode("1")) | |
| 1053 etagb = etag.strip ('"') | |
| 1054 env = {"CONTENT_TYPE": "application/json", | |
| 1055 "CONTENT_LEN": 0, | |
| 1056 "REQUEST_METHOD": "DELETE" } | |
| 1057 # use text/plain header and request json output by appending | |
| 1058 # .json to the url. | |
| 1059 headers={"accept": "text/plain", | |
| 1060 "content-type": env['CONTENT_TYPE'], | |
| 1061 "if-match": '"%s"'%etagb, | |
| 1062 "content-length": 0, | |
| 1063 } | |
| 1064 self.headers=headers | |
| 1065 body_file=BytesIO(b'') # FieldStorage needs a file | |
| 1066 form = client.BinaryFieldStorage(body_file, | |
| 1067 headers=headers, | |
| 1068 environ=env) | |
| 1069 self.server.client.request.headers.get=self.get_header | |
| 1070 self.db.setCurrentUser('admin') # must be admin to delete issue | |
| 1071 results = self.server.dispatch('DELETE', | |
| 1072 "/rest/data/issue/1.json", | |
| 1073 form) | |
| 1074 self.assertEqual(self.server.client.response_code, 200) | |
| 1075 json_dict = json.loads(b2s(results)) | |
| 1076 print(results) | |
| 1077 status=json_dict['data']['status'] | |
| 1078 status=json_dict['data']['status'] | |
| 1079 self.assertEqual(status, 'ok') | |
| 1080 | |
| 1081 del(self.headers) | |
| 1082 | |
| 1083 def testPostPOE(self): | |
| 1084 ''' test post once exactly: get POE url, create issue | |
| 1085 using POE url. Use dispatch entry point. | |
| 1086 ''' | |
| 1087 import time | |
| 1088 # setup environment | |
| 1089 etag = "not needed" | |
| 1090 empty_body=b'' | |
| 1091 env = { "CONTENT_TYPE": "application/json", | |
| 1092 "CONTENT_LENGTH": len(empty_body), | |
| 1093 "REQUEST_METHOD": "POST" | |
| 1094 } | |
| 1095 headers={"accept": "application/json", | |
| 1096 "content-type": env['CONTENT_TYPE'], | |
| 1097 "content-length": len(empty_body) | |
| 1098 } | |
| 1099 self.headers=headers | |
| 1100 # use empty_body to test code path for missing/empty json | |
| 1101 body_file=BytesIO(empty_body) # FieldStorage needs a file | |
| 1102 form = client.BinaryFieldStorage(body_file, | |
| 1103 headers=headers, | |
| 1104 environ=env) | |
| 1012 | 1105 |
| 1013 ## Obtain the POE url. | 1106 ## Obtain the POE url. |
| 1014 self.server.client.request.headers.get=self.get_header | 1107 self.server.client.request.headers.get=self.get_header |
| 1015 results = self.server.dispatch('POST', | 1108 results = self.server.dispatch('POST', |
| 1016 "/rest/data/issue/@poe", | 1109 "/rest/data/issue/@poe", |
| 1022 | 1115 |
| 1023 # strip tracker web prefix leaving leading /. | 1116 # strip tracker web prefix leaving leading /. |
| 1024 url = url[len(self.db.config['TRACKER_WEB'])-1:] | 1117 url = url[len(self.db.config['TRACKER_WEB'])-1:] |
| 1025 | 1118 |
| 1026 ## create an issue using poe url. | 1119 ## create an issue using poe url. |
| 1120 body=b'{ "title": "foo bar", "priority": "critical" }' | |
| 1121 env = { "CONTENT_TYPE": "application/json", | |
| 1122 "CONTENT_LENGTH": len(body), | |
| 1123 "REQUEST_METHOD": "POST" | |
| 1124 } | |
| 1125 headers={"accept": "application/json", | |
| 1126 "content-type": env['CONTENT_TYPE'], | |
| 1127 "content-length": len(body) | |
| 1128 } | |
| 1129 self.headers=headers | |
| 1130 body_file=BytesIO(body) # FieldStorage needs a file | |
| 1131 form = client.BinaryFieldStorage(body_file, | |
| 1132 headers=headers, | |
| 1133 environ=env) | |
| 1027 self.server.client.request.headers.get=self.get_header | 1134 self.server.client.request.headers.get=self.get_header |
| 1028 results = self.server.dispatch('POST', | 1135 results = self.server.dispatch('POST', |
| 1029 url, | 1136 url, |
| 1030 form) | 1137 form) |
| 1031 | 1138 |
