|
| 1 | +import requests |
| 2 | +import random |
| 3 | +import string |
| 4 | +from config import BASE_URL, API_TOKEN |
| 5 | + |
| 6 | +def _headers(): |
| 7 | + return {"Authorization": f"token {API_TOKEN}"} |
| 8 | + |
| 9 | +def _rand(prefix): |
| 10 | + return prefix + "_" + "".join(random.choices(string.ascii_lowercase + string.digits, k=6)) |
| 11 | + |
| 12 | +def test_create_issue_and_list_it(): |
| 13 | + headers = _headers() |
| 14 | + |
| 15 | + # 1) Create owner user |
| 16 | + username = _rand("owner") |
| 17 | + user_payload = { |
| 18 | + "email": f"{username}@example.com", |
| 19 | + "username": username, |
| 20 | + "password": "TestPass123!", |
| 21 | + "must_change_password": False, |
| 22 | + "send_notify": False, |
| 23 | + } |
| 24 | + r_user = requests.post(f"{BASE_URL}/api/v1/admin/users", headers=headers, json=user_payload) |
| 25 | + assert r_user.status_code == 201, f"User create failed: {r_user.status_code} {r_user.text}" |
| 26 | + |
| 27 | + # 2) Create repo |
| 28 | + repo_name = _rand("repo") |
| 29 | + repo_payload = {"name": repo_name, "description": "Issues test", "private": False, "auto_init": True} |
| 30 | + r_repo = requests.post(f"{BASE_URL}/api/v1/admin/users/{username}/repos", headers=headers, json=repo_payload) |
| 31 | + assert r_repo.status_code == 201, f"Repo create failed: {r_repo.status_code} {r_repo.text}" |
| 32 | + |
| 33 | + # 3) Create an issue |
| 34 | + title = f"Issue { _rand('t') }" |
| 35 | + body = "Created by API test" |
| 36 | + issue_payload = {"title": title, "body": body} |
| 37 | + r_issue = requests.post( |
| 38 | + f"{BASE_URL}/api/v1/repos/{username}/{repo_name}/issues", |
| 39 | + headers=headers, |
| 40 | + json=issue_payload |
| 41 | + ) |
| 42 | + assert r_issue.status_code == 201, f"Issue create failed: {r_issue.status_code} {r_issue.text}" |
| 43 | + issue = r_issue.json() |
| 44 | + number = issue.get("number") |
| 45 | + assert number, f"Expected issue number, got {issue}" |
| 46 | + assert issue["title"] == title |
| 47 | + assert issue["state"] == "open" |
| 48 | + |
| 49 | + # 4) List issues and verify the created one is present |
| 50 | + r_list = requests.get(f"{BASE_URL}/api/v1/repos/{username}/{repo_name}/issues", headers=headers) |
| 51 | + assert r_list.status_code == 200, f"Issues list failed: {r_list.status_code} {r_list.text}" |
| 52 | + issues = r_list.json() |
| 53 | + numbers = {i.get("number") for i in issues} |
| 54 | + assert number in numbers, f"Issue #{number} not found in list: {numbers}" |
| 55 | + |
| 56 | + # 5) Cleanup (best-effort) |
| 57 | + requests.delete(f"{BASE_URL}/api/v1/repos/{username}/{repo_name}", headers=headers) |
| 58 | + requests.delete(f"{BASE_URL}/api/v1/admin/users/{username}", headers=headers) |
0 commit comments