|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import time |
| 4 | + |
| 5 | +THIS_FOLDER = os.path.dirname(__file__) |
| 6 | +sys.path.append(os.path.join(THIS_FOLDER, "../../tools/")) |
| 7 | +from tester import Tester |
| 8 | + |
| 9 | + |
| 10 | +class TestTaggedPosts: |
| 11 | + def __init__(self): |
| 12 | + self.tester = Tester(headless=True) |
| 13 | + self.url = self.tester.start_py4web(THIS_FOLDER, port=8888) |
| 14 | + self.cookies = None |
| 15 | + |
| 16 | + def run(self): |
| 17 | + self.tester.run_steps(self) |
| 18 | + |
| 19 | + def step_01(self): |
| 20 | + "check we can open the page" |
| 21 | + self.tester.open(self.url) |
| 22 | + self.tester.notify("Success in opening page") |
| 23 | + |
| 24 | + def step_02(self): |
| 25 | + "check login" |
| 26 | + user = dict( |
| 27 | + username="tester", |
| 28 | + email="tester@example.com", |
| 29 | + password="1234qwerQWER!@#$", |
| 30 | + first_name="Tester", |
| 31 | + last_name="TESTER", |
| 32 | + ) |
| 33 | + self.tester.create_user(user) |
| 34 | + self.tester.auth_sign_in(user) |
| 35 | + self.tester.open(self.url) |
| 36 | + assert "tester" in self.tester.browser.page_source, "unable to login" |
| 37 | + assert "logout" in self.tester.browser.page_source, "unable to login" |
| 38 | + set_cookies = self.tester.browser.get_cookies() |
| 39 | + assert len(set_cookies) >= 1, "server cookies not working" |
| 40 | + self.cookies = {"tagged_posts_session": set_cookies[0]["value"]} |
| 41 | + db = self.tester.app_as_module.db |
| 42 | + assert "post_item" in db.tables, "table post_item not found in models.py" |
| 43 | + post_item = db.post_item |
| 44 | + assert "content" in post_item.fields, "post_item has no content field" |
| 45 | + assert "created_on" in post_item.fields, "post_item has no created_on field" |
| 46 | + assert "created_by" in post_item.fields, "post_item has no created_by field" |
| 47 | + assert ( |
| 48 | + post_item.created_on.type == "datetime" |
| 49 | + ), "post_item.created_on must be a datetime" |
| 50 | + assert ( |
| 51 | + post_item.created_by.type == "reference auth_user" |
| 52 | + ), "post_item.created_by must be a reference" |
| 53 | + self.tester.notify("Table post_item defined correctly", score=1.0) |
| 54 | + |
| 55 | + assert "tag_item" in db.tables, "table tag_item not found in models.py" |
| 56 | + tag_item = db.tag_item |
| 57 | + assert "name" in tag_item.fields, "tag_item has no name field" |
| 58 | + assert "post_item_id" in tag_item.fields, "tag_item has no post_item_id field" |
| 59 | + assert ( |
| 60 | + tag_item.post_item_id.type == "reference post_item" |
| 61 | + ), "tag_item.post_item_id must be a reference" |
| 62 | + self.tester.notify("Table tag_item defined correctly", score=1.0) |
| 63 | + |
| 64 | + def step_03(self): |
| 65 | + "check api" |
| 66 | + if not self.cookies: |
| 67 | + self.tester.notify("Cannot proceed if unable to login") |
| 68 | + self.tester.stop() |
| 69 | + # self.url = "http://127.0.0.1:8000/bird_spotter/" |
| 70 | + try: |
| 71 | + content = "This is a message about #fun #games" |
| 72 | + res = self.tester.fetch( |
| 73 | + "POST", |
| 74 | + self.url + "api/posts", |
| 75 | + {"content": content}, |
| 76 | + ) |
| 77 | + assert res.get("id") == 1, "unable to store a post_item using API" |
| 78 | + except: |
| 79 | + pass |
| 80 | + else: |
| 81 | + assert False, "I should not have been able to access API without Login" |
| 82 | + |
| 83 | + content = "This is a message about #fun #games" |
| 84 | + res = self.tester.fetch( |
| 85 | + "POST", |
| 86 | + self.url + "api/posts", |
| 87 | + {"content": content}, |
| 88 | + cookies=self.cookies, |
| 89 | + ) |
| 90 | + assert res.get("id") == 1, "unable to store a post_item using API" |
| 91 | + |
| 92 | + time.sleep(1) |
| 93 | + |
| 94 | + content = "This is a message about #boring #games" |
| 95 | + res = self.tester.fetch( |
| 96 | + "POST", |
| 97 | + self.url + "api/posts", |
| 98 | + {"content": content}, |
| 99 | + cookies=self.cookies, |
| 100 | + ) |
| 101 | + assert res.get("id") == 2, "unable to store a post_item using API" |
| 102 | + self.tester.notify("POST to /api/posts works", score=1.0) |
| 103 | + |
| 104 | + res = self.tester.fetch("GET", self.url + "api/tags", cookies=self.cookies) |
| 105 | + assert res == { |
| 106 | + "tags": ["boring", "fun", "games"] |
| 107 | + }, "Did not receive correct tags" |
| 108 | + self.tester.notify("GET to /api/tags works", score=1.0) |
| 109 | + |
| 110 | + res = self.tester.fetch("GET", self.url + "api/posts", cookies=self.cookies) |
| 111 | + assert "posts" in res, 'expected {"posts": [...]}' |
| 112 | + assert len(res["posts"]) == 2, "expected to posts in response" |
| 113 | + assert ( |
| 114 | + "#boring" in res["posts"][0]["content"] |
| 115 | + ), "expected the first post to containt #boring" |
| 116 | + assert ( |
| 117 | + "#fun" in res["posts"][1]["content"] |
| 118 | + ), "expected the second post to containt #boring" |
| 119 | + self.tester.notify("GET to /api/posts works", score=0.4) |
| 120 | + |
| 121 | + res = self.tester.fetch( |
| 122 | + "GET", self.url + "api/posts?tags=fun", cookies=self.cookies |
| 123 | + ) |
| 124 | + assert "posts" in res, 'expected {"posts": [...]}' |
| 125 | + assert len(res["posts"]) == 1, "expected to posts in response" |
| 126 | + assert ( |
| 127 | + "#fun" in res["posts"][0]["content"] |
| 128 | + ), "expected the second post to containt #boring" |
| 129 | + self.tester.notify("GET to /api/posts?tags=fun works", score=0.3) |
| 130 | + |
| 131 | + res = self.tester.fetch( |
| 132 | + "GET", self.url + "api/posts?tags=fun,boring", cookies=self.cookies |
| 133 | + ) |
| 134 | + assert "posts" in res, 'expected {"posts": [...]}' |
| 135 | + assert len(res["posts"]) == 2, "expected to posts in response" |
| 136 | + self.tester.notify("GET to /api/posts?tags=fun,boring works", score=0.3) |
| 137 | + |
| 138 | + res = self.tester.fetch( |
| 139 | + "DELETE", self.url + "api/posts/1", cookies=self.cookies |
| 140 | + ) |
| 141 | + db = self.tester.app_as_module.db |
| 142 | + assert db(db.post_item).count() == 1, "unable to delete post" |
| 143 | + self.tester.notify("DELETE to /api/posts works", score=1.0) |
| 144 | + |
| 145 | + def step_04(self): |
| 146 | + """check post items""" |
| 147 | + self.tester.open(self.url) |
| 148 | + self.tester.find_first("textarea.post-content") |
| 149 | + self.tester.find_first("button.submit-content") |
| 150 | + self.tester.find_first(".feed") |
| 151 | + items = self.tester.find_all(".feed .post_item") |
| 152 | + assert len(items) == 1, "Expected to find one post_item" |
| 153 | + assert "#boring" in items[0].get_attribute( |
| 154 | + "innerHTML" |
| 155 | + ), "Exepcted to find a post_item" |
| 156 | + self.tester.notify("Feed column works", score=1.0) |
| 157 | + |
| 158 | + def step_05(self): |
| 159 | + """check tags""" |
| 160 | + self.tester.find_first(".tags") |
| 161 | + tags = self.tester.find_all(".tags .tag") |
| 162 | + assert len(tags) == 2, "did not find the expected tags" |
| 163 | + assert "boring" in tags[0].text, "Exepcted the boring tag" |
| 164 | + assert "games" in tags[1].text, "Exepcted the games tag" |
| 165 | + self.tester.notify("Tags column works", score=1.0) |
| 166 | + |
| 167 | + def step_06(self): |
| 168 | + """check filter by tags""" |
| 169 | + self.tester.open(self.url) |
| 170 | + content = "#hello #world" |
| 171 | + self.tester.find_first("textarea.post-content").send_keys(content) |
| 172 | + self.tester.find_first("button.submit-content").click() |
| 173 | + time.sleep(1) |
| 174 | + |
| 175 | + db = self.tester.app_as_module.db |
| 176 | + assert db(db.post_item).count() == 2, "record not inserted in database" |
| 177 | + self.tester.notify("Posting from page works", score=0.5) |
| 178 | + |
| 179 | + self.tester.find_first(".feed") |
| 180 | + items = self.tester.find_all(".feed .post_item") |
| 181 | + assert len(items) == 2, "Exepcted to find two post_items" |
| 182 | + assert "#hello" in items[0].get_attribute( |
| 183 | + "innerHTML" |
| 184 | + ), "Exepcted to find a post_item" |
| 185 | + assert "#world" in items[0].get_attribute( |
| 186 | + "innerHTML" |
| 187 | + ), "Exepcted to find a post_item" |
| 188 | + self.tester.notify("Posting to the feed works", score=0.5) |
| 189 | + |
| 190 | + tags = self.tester.find_all(".tags .tag") |
| 191 | + assert "boring" in tags[0].text, "Exepcted the boring tag" |
| 192 | + assert "games" in tags[1].text, "Exepcted the games tag" |
| 193 | + assert "hello" in tags[2].text, "Exepcted the hello tag" |
| 194 | + assert "world" in tags[3].text, "Exepcted the world tag" |
| 195 | + self.tester.notify("Tags refreshed correclty", score=1.0) |
| 196 | + |
| 197 | + tags[0].click() |
| 198 | + time.sleep(1) |
| 199 | + items = self.tester.find_all(".feed .post_item") |
| 200 | + assert len(items) == 1, "Exepcted to find one post_item" |
| 201 | + assert "#boring" in items[0].get_attribute( |
| 202 | + "innerHTML" |
| 203 | + ), "Exepcted to find a post_item" |
| 204 | + assert "#games" in items[0].get_attribute( |
| 205 | + "innerHTML" |
| 206 | + ), "Exepcted to find a post_item" |
| 207 | + self.tester.notify("Tags toggling works", score=0.5) |
| 208 | + |
| 209 | + tags[0].click() |
| 210 | + time.sleep(1) |
| 211 | + items = self.tester.find_all(".feed .post_item") |
| 212 | + assert len(items) == 2, "Exepcted to find two post_item" |
| 213 | + self.tester.notify("Tags untoggling works", score=0.5) |
| 214 | + |
| 215 | + def step_07(self): |
| 216 | + """check delete item""" |
| 217 | + self.tester.open(self.url) |
| 218 | + items = self.tester.find_all(".feed .post_item") |
| 219 | + buttons = self.tester.find_all(".feed .post_item button") |
| 220 | + assert len(items) == 2, "Expected two post_items" |
| 221 | + assert len(buttons) == 2, "Expected a delete button per item" |
| 222 | + buttons[0].click() |
| 223 | + time.sleep(1) |
| 224 | + items = self.tester.find_all(".feed .post_item") |
| 225 | + assert len(items) == 1, "Expected the item to be deleted" |
| 226 | + self.tester.open(self.url) |
| 227 | + items = self.tester.find_all(".feed .post_item") |
| 228 | + assert len(items) == 1, "Expected the item to be deleted" |
| 229 | + self.tester.notify("Delete using the feed button works", score=1.0) |
| 230 | + |
| 231 | + |
| 232 | +if __name__ == "__main__": |
| 233 | + TestTaggedPosts().run() |
0 commit comments