Skip to content

Commit 27de0ac

Browse files
committed
added new tests
1 parent a075ef7 commit 27de0ac

File tree

7 files changed

+219
-2
lines changed

7 files changed

+219
-2
lines changed

.github/workflows/api-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ jobs:
3131
env:
3232
API_TOKEN: ${{ secrets.GITEA_API_TOKEN }}
3333
run: |
34-
echo "BASE_URL = 'http://108.130.122.66:3000/'" > config.py
34+
echo "BASE_URL = 'http://52.211.112.5:3000/'" > config.py
3535
echo "API_TOKEN = '${API_TOKEN}'" >> config.py
3636
pytest tests/api -v

tests/api/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
API_TOKEN = 'f1600abdf735b9e11dbfc45e57db04319d58235e'
88

99
#BASE_URL = os.getenv('GITEA_URL', 'http://localhost:3000')
10-
BASE_URL = 'http://108.130.122.66:3000'
10+
BASE_URL = 'http://52.211.112.5:3000'

tests/api/test_auth.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import requests
2+
from config import BASE_URL, API_TOKEN # uses the same config.py you already generate in CI
3+
4+
def test_me_endpoint_returns_current_user_and_is_admin():
5+
headers = {"Authorization": f"token {API_TOKEN}"}
6+
7+
r = requests.get(f"{BASE_URL}/api/v1/user", headers=headers)
8+
assert r.status_code == 200, f"Unexpected status: {r.status_code} body={r.text}"
9+
10+
me = r.json()
11+
# Basic shape checks
12+
for key in ["id", "login", "email", "is_admin", "created"]:
13+
assert key in me, f"Missing key '{key}' in /user response: {me}"
14+
15+
# If the token is an admin (your token is), this should be True
16+
assert me["is_admin"] is True

tests/api/test_branches.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_list_branches_contains_default_branch():
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 with auto_init so default branch exists
28+
repo_name = _rand("repo")
29+
repo_payload = {"name": repo_name, "description": "Branches 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) Get repo to read its default_branch
34+
r_get = requests.get(f"{BASE_URL}/api/v1/repos/{username}/{repo_name}", headers=headers)
35+
assert r_get.status_code == 200, f"Get repo failed: {r_get.status_code} {r_get.text}"
36+
default_branch = r_get.json().get("default_branch")
37+
assert default_branch, "Expected default_branch to be set"
38+
39+
# 4) List branches and verify default branch is present
40+
r_branches = requests.get(f"{BASE_URL}/api/v1/repos/{username}/{repo_name}/branches", headers=headers)
41+
assert r_branches.status_code == 200, f"List branches failed: {r_branches.status_code} {r_branches.text}"
42+
branches = r_branches.json()
43+
assert isinstance(branches, list) and branches, "Expected non-empty branches list"
44+
45+
names = {b.get("name") for b in branches}
46+
assert default_branch in names, f"default branch '{default_branch}' not found in {names}"
47+
48+
# 5) Cleanup (best-effort)
49+
requests.delete(f"{BASE_URL}/api/v1/repos/{username}/{repo_name}", headers=headers)
50+
requests.delete(f"{BASE_URL}/api/v1/admin/users/{username}", headers=headers)

tests/api/test_duplicate_users.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import requests
2+
import string
3+
import random
4+
from config import BASE_URL, API_TOKEN
5+
6+
def _headers():
7+
return {"Authorization": f"token {API_TOKEN}"}
8+
9+
def _rand_username(prefix="dupuser"):
10+
return prefix + "_" + "".join(random.choices(string.ascii_lowercase + string.digits, k=6))
11+
12+
def test_create_duplicate_user_should_fail():
13+
headers = _headers()
14+
username = _rand_username()
15+
payload = {
16+
"email": f"{username}@example.com",
17+
"username": username,
18+
"password": "TestPass123!",
19+
"must_change_password": False,
20+
"send_notify": False,
21+
}
22+
23+
# First creation should succeed
24+
r1 = requests.post(f"{BASE_URL}/api/v1/admin/users", headers=headers, json=payload)
25+
assert r1.status_code == 201, f"First create failed: {r1.status_code} {r1.text}"
26+
27+
# Second creation with the same username should fail
28+
r2 = requests.post(f"{BASE_URL}/api/v1/admin/users", headers=headers, json=payload)
29+
30+
# Gitea typically returns 422 for duplicate username; some setups may return 409
31+
assert r2.status_code in (422, 409), f"Expected 422/409 on duplicate, got {r2.status_code} {r2.text}"
32+
33+
# Error body should hint user exists
34+
err_text = r2.text.lower()
35+
assert ("exist" in err_text) or ("already" in err_text), f"Unexpected error message: {r2.text}"
36+
37+
# Cleanup: best-effort delete the user we created (ignore errors)
38+
requests.delete(f"{BASE_URL}/api/v1/admin/users/{username}", headers=headers)

tests/api/test_issues.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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)

tests/api/test_repos.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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(s="user"):
10+
return s + "_" + "".join(random.choices(string.ascii_lowercase + string.digits, k=6))
11+
12+
def test_create_repo_then_get_details():
13+
headers = _headers()
14+
15+
# 1) Create a user (owner)
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 a repository for that user
28+
repo_name = _rand("repo")
29+
repo_payload = {
30+
"name": repo_name,
31+
"description": "API test repo",
32+
"private": False,
33+
"auto_init": True, # so default branch & README exist
34+
}
35+
r_repo = requests.post(f"{BASE_URL}/api/v1/admin/users/{username}/repos", headers=headers, json=repo_payload)
36+
assert r_repo.status_code == 201, f"Repo create failed: {r_repo.status_code} {r_repo.text}"
37+
created = r_repo.json()
38+
assert created["name"] == repo_name
39+
assert created["owner"]["login"] == username
40+
41+
# 3) GET repo details and verify fields
42+
r_get = requests.get(f"{BASE_URL}/api/v1/repos/{username}/{repo_name}", headers=headers)
43+
assert r_get.status_code == 200, f"Get repo failed: {r_get.status_code} {r_get.text}"
44+
repo = r_get.json()
45+
46+
# Basic shape checks
47+
assert repo["name"] == repo_name
48+
assert repo["owner"]["login"] == username
49+
assert repo["private"] is False
50+
# default branch commonly 'main' or 'master' depending on settings; assert it's non-empty
51+
assert repo.get("default_branch"), f"Expected default_branch to be set, got {repo.get('default_branch')}"
52+
53+
# 4) Cleanup (best-effort)
54+
requests.delete(f"{BASE_URL}/api/v1/repos/{username}/{repo_name}", headers=headers)
55+
requests.delete(f"{BASE_URL}/api/v1/admin/users/{username}", headers=headers)

0 commit comments

Comments
 (0)