|
1 | 1 | from tests.conftest import client |
2 | | -from tests import db, User |
3 | | -from tests.api import create_user_for_test_cases |
| 2 | +from tests import db, User, UserLink |
| 3 | +from tests.api import create_user_for_test_cases, create_user_link_for_test_cases |
4 | 4 |
|
5 | 5 | class TestUserView(object): |
6 | 6 |
|
@@ -84,17 +84,77 @@ def test_get_all_users(self, client): |
84 | 84 | assert response.status_code == 200 |
85 | 85 | assert response.get_json() == [users_dict[1]] |
86 | 86 |
|
87 | | - # notice: Why do we need name in UserLink? |
88 | | - # notice: Won't the API will provide the UserLink, then why the endpoint expects a UserLink specification? |
89 | 87 | def test_create_user_link(self, client): |
90 | 88 | user = create_user_for_test_cases(self.valid_data) |
91 | 89 | url = '/users/{}/links'.format(user["id"]) |
92 | 90 |
|
93 | 91 | response = client.post(url, json={"user_id": user["id"]}) |
94 | 92 | assert response.status_code == 400 |
95 | 93 |
|
96 | | - # response = client.post(url, ) |
97 | | - # assert 1 == 1 |
| 94 | + response = client.post(url, json={"name": "Smedia", "url": "http://s.media"}) |
| 95 | + assert response.status_code == 201 |
| 96 | + |
| 97 | + response = client.post(url, json={"name": None, "url": "http://l.co"}) |
| 98 | + assert response.status_code == 400 |
| 99 | + |
| 100 | + response = client.post(url, json={"name": "WORK"}) |
| 101 | + assert response.status_code == 400 |
| 102 | + |
| 103 | + def test_update_user_link(self, client): |
| 104 | + user = create_user_for_test_cases(self.valid_data) |
| 105 | + link = create_user_link_for_test_cases({"name": "Discord", "url": "http://dc.com/my", "user_id": user["id"]}) |
| 106 | + |
| 107 | + url = "/users/{0}/links/{1}".format(0, link["id"]) |
| 108 | + |
| 109 | + response = client.post(url, json={"name": "Portfolio"}) |
| 110 | + assert response.status_code == 400 |
| 111 | + |
| 112 | + url = "/users/{0}/links/{1}".format(user["id"], link["id"]) |
| 113 | + |
| 114 | + response = client.post(url, json={"user_id": 0}) |
| 115 | + assert response.status_code == 400 |
| 116 | + |
| 117 | + response = client.post(url, json={"link_id": 1}) |
| 118 | + assert response.status_code == 400 |
| 119 | + |
| 120 | + # notice: Should we respond to update_user request without json data with status code 200? |
| 121 | + # response = client.post(url, json={}) |
| 122 | + # assert response.status_code == 400 |
| 123 | + |
| 124 | + response = client.post(url, json={"name": "New Name"}) |
| 125 | + assert response.status_code == 200 |
| 126 | + assert response.get_json()["name"] == "New Name" |
| 127 | + |
| 128 | + def test_get_all_user_links(self, client): |
| 129 | + user = create_user_for_test_cases(self.valid_data) |
| 130 | + link1 = create_user_link_for_test_cases({"name": "Discord", "url": "http://dc.com/my", "user_id": user["id"]}) |
| 131 | + link2 = create_user_link_for_test_cases({"name": "PB", "url": "http://pb.com/ur234", "user_id": user["id"]}) |
| 132 | + |
| 133 | + response = client.get("/users/{}/links".format(user["id"])) |
| 134 | + assert response.status_code == 200 |
| 135 | + assert response.get_json() == [link1, link2] |
| 136 | + |
| 137 | + def test_delete_user_link(self, client): |
| 138 | + user = create_user_for_test_cases(self.valid_data) |
| 139 | + link1 = UserLink(**{"name": "Discord", "url": "http://dc.com/my", "user_id": user["id"]}) |
| 140 | + link2 = UserLink(**{"name": "PB", "url": "http://pb.com/ur234", "user_id": user["id"]}) |
| 141 | + |
| 142 | + db.session.add(link1) |
| 143 | + db.session.add(link2) |
| 144 | + db.session.commit() |
| 145 | + |
| 146 | + response = client.delete("/users/{0}/links/{1}".format(0, link1.id)) |
| 147 | + assert response.status_code == 404 |
| 148 | + |
| 149 | + response = client.delete("/users/{0}/links/{1}".format(user["id"], 0)) |
| 150 | + assert response.status_code == 404 |
| 151 | + |
| 152 | + response = client.delete("/users/{0}/links/{1}".format(user["id"], link1.id)) |
| 153 | + assert response.status_code == 200 |
| 154 | + |
| 155 | + recorded_links = UserLink.query.all() |
| 156 | + assert len(recorded_links) == 1 |
| 157 | + assert link1 not in recorded_links |
98 | 158 |
|
99 | 159 | def test_create_user_feedback(self, client): |
100 | 160 | user1 = create_user_for_test_cases(self.valid_data) |
|
0 commit comments