|
| 1 | + |
| 2 | +""" |
| 3 | + Copyright(c) 2021 the original author or authors |
| 4 | +
|
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | +
|
| 9 | + https: // www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 14 | + or implied. See the License for the specific language governing |
| 15 | + permissions and limitations under the License. |
| 16 | + """ |
| 17 | +import json |
| 18 | +import httpretty |
| 19 | +import requests |
| 20 | +from compreface.client import SubjectClient |
| 21 | +from compreface.config.api_list import SUBJECTS_CRUD_API |
| 22 | +from tests.client.const_config import DOMAIN, PORT, RECOGNIZE_API_KEY |
| 23 | +""" |
| 24 | + Server configuration |
| 25 | +""" |
| 26 | +url: str = DOMAIN + ":" + PORT + SUBJECTS_CRUD_API |
| 27 | + |
| 28 | + |
| 29 | +@httpretty.activate(verbose=True, allow_net_connect=False) |
| 30 | +def test_get(): |
| 31 | + httpretty.register_uri( |
| 32 | + httpretty.GET, |
| 33 | + url, |
| 34 | + headers={'x-api-key': RECOGNIZE_API_KEY}, |
| 35 | + body='{"subjects": ["Subject", "Subject2"]}' |
| 36 | + ) |
| 37 | + test_subject: SubjectClient = SubjectClient(RECOGNIZE_API_KEY, DOMAIN, PORT) |
| 38 | + response: dict = requests.get(url=url, headers={'x-api-key': RECOGNIZE_API_KEY}).json() |
| 39 | + test_response: dict = test_subject.get() |
| 40 | + assert response == test_response |
| 41 | + |
| 42 | + |
| 43 | +@httpretty.activate(verbose=True, allow_net_connect=False) |
| 44 | +def test_post(): |
| 45 | + httpretty.register_uri( |
| 46 | + httpretty.POST, |
| 47 | + url, |
| 48 | + headers={'x-api-key': RECOGNIZE_API_KEY, |
| 49 | + 'Content-Type': 'application/json'}, |
| 50 | + body='{"subject": "Subject"}' |
| 51 | + ) |
| 52 | + |
| 53 | + data = {'subject': 'Subject'} |
| 54 | + |
| 55 | + response: dict = requests.post( |
| 56 | + url=url, data=json.dumps(data), headers={'x-api-key': RECOGNIZE_API_KEY, |
| 57 | + 'Content-Type': 'application/json'}).json() |
| 58 | + |
| 59 | + test_subject: SubjectClient = SubjectClient(RECOGNIZE_API_KEY, DOMAIN, PORT) |
| 60 | + test_response: dict = test_subject.post({'subject': 'Subject'}) |
| 61 | + assert response == test_response |
| 62 | + |
| 63 | + |
| 64 | +@httpretty.activate(verbose=True, allow_net_connect=False) |
| 65 | +def test_post_incorrect_response(): |
| 66 | + httpretty.register_uri( |
| 67 | + httpretty.POST, |
| 68 | + url, |
| 69 | + headers={'x-api-key': RECOGNIZE_API_KEY, |
| 70 | + 'Content-Type': 'application/json'}, |
| 71 | + body='{"subject": "Subjectss"}' |
| 72 | + ) |
| 73 | + |
| 74 | + data = {'subject': 'Subjectss'} |
| 75 | + |
| 76 | + response: dict = requests.post( |
| 77 | + url=url, data=json.dumps(data), headers={'x-api-key': RECOGNIZE_API_KEY, |
| 78 | + 'Content-Type': 'application/json'}).json() |
| 79 | + |
| 80 | + test_subject: SubjectClient = SubjectClient(RECOGNIZE_API_KEY, DOMAIN, PORT) |
| 81 | + test_response: dict = test_subject.post({'subject': 'Subject'}) |
| 82 | + assert response != test_response |
| 83 | + |
| 84 | + |
| 85 | +@httpretty.activate(verbose=True, allow_net_connect=False) |
| 86 | +def test_delete(): |
| 87 | + httpretty.register_uri( |
| 88 | + httpretty.DELETE, |
| 89 | + url + '/Subject', |
| 90 | + headers={'x-api-key': RECOGNIZE_API_KEY, |
| 91 | + 'Content-Type': 'application/json'}, |
| 92 | + body='{"subject": "Subject"}' |
| 93 | + ) |
| 94 | + |
| 95 | + response = requests.delete( |
| 96 | + url=url, headers={'x-api-key': RECOGNIZE_API_KEY, |
| 97 | + 'Content-Type': 'application/json'}) |
| 98 | + |
| 99 | + test_subject: SubjectClient = SubjectClient(RECOGNIZE_API_KEY, DOMAIN, PORT) |
| 100 | + test_response = test_subject.delete("Subject") |
| 101 | + assert response == test_response |
| 102 | + |
| 103 | + |
| 104 | +@httpretty.activate(verbose=True, allow_net_connect=False) |
| 105 | +def test_put(): |
| 106 | + httpretty.register_uri( |
| 107 | + httpretty.PUT, |
| 108 | + url + '/Subject', |
| 109 | + headers={'x-api-key': RECOGNIZE_API_KEY, |
| 110 | + 'Content-Type': 'application/json'}, |
| 111 | + body='{"subject": "NewSubject"}' |
| 112 | + ) |
| 113 | + |
| 114 | + data = {"subject": "NewSubject"} |
| 115 | + |
| 116 | + response: dict = requests.put( |
| 117 | + url=url, data=json.dumps(data), headers={'x-api-key': RECOGNIZE_API_KEY}).json() |
| 118 | + |
| 119 | + test_subject: SubjectClient = SubjectClient(RECOGNIZE_API_KEY, DOMAIN, PORT) |
| 120 | + test_response: dict = test_subject.put({"subject": "NewSubject", "api_endpoint": "Subject"}) |
| 121 | + assert response == test_response |
0 commit comments