Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: create a global conftest.py (Bug 1942851) #199

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 1 addition & 91 deletions src/lando/api/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import json
import os
import subprocess
import time
from datetime import datetime, timezone
from pathlib import Path

import pytest
import redis
import requests
import requests_mock
from django.conf import settings
from django.contrib.auth.models import User
from django.core.cache import cache
from django.http import HttpResponse
from django.http import JsonResponse as JSONResponse
Expand All @@ -27,7 +23,7 @@
)
from lando.api.legacy.transplants import CODE_FREEZE_OFFSET, tokens_are_equal
from lando.api.tests.mocks import PhabricatorDouble, TreeStatusDouble
from lando.main.models import SCM_LEVEL_1, SCM_LEVEL_3, Profile, Repo
from lando.main.models import SCM_LEVEL_1, SCM_LEVEL_3, Repo
from lando.main.scm import SCM_TYPE_HG
from lando.main.support import LegacyAPIException
from lando.main.tests.conftest import git_repo, git_repo_seed
Expand Down Expand Up @@ -388,55 +384,6 @@ def patch_directory(request):
return Path(request.fspath.dirname).joinpath("patches")


@pytest.fixture
def hg_test_bundle(request):
return Path(request.fspath.dirname).joinpath("data", "test-repo.bundle")


@pytest.fixture
def hg_server(hg_test_bundle, tmpdir):
# TODO: Select open port.
port = "8000"
hg_url = "http://localhost:" + port

repo_dir = tmpdir.mkdir("hg_server")
subprocess.run(["hg", "clone", hg_test_bundle, repo_dir], check=True, cwd="/")

serve = subprocess.Popen(
[
"hg",
"serve",
"--config",
"web.push_ssl=False",
"--config",
"web.allow_push=*",
"-p",
port,
"-R",
repo_dir,
]
)
if serve.poll() is not None:
raise Exception("Failed to start the mercurial server.")
# Wait until the server is running.
for _i in range(10):
try:
requests.get(hg_url)
except Exception:
time.sleep(1)
break

yield hg_url
serve.kill()


@pytest.fixture
def hg_clone(hg_server, tmpdir):
clone_dir = tmpdir.join("hg_clone")
subprocess.run(["hg", "clone", hg_server, clone_dir.strpath], check=True)
return clone_dir


@pytest.fixture
def register_codefreeze_uri(request_mocker):
request_mocker.register_uri(
Expand Down Expand Up @@ -625,43 +572,6 @@ def put(self, path, **kwargs):
return ProxyClient()


@pytest.fixture
def conduit_permissions():
permissions = (
"scm_level_1",
"scm_level_2",
"scm_level_3",
"scm_conduit",
)
all_perms = Profile.get_all_scm_permissions()

return [all_perms[p] for p in permissions]


@pytest.fixture
def user_plaintext_password():
return "test_password"


@pytest.fixture
def user(user_plaintext_password, conduit_permissions):
user = User.objects.create_user(
username="test_user",
password=user_plaintext_password,
email="[email protected]",
)

user.profile = Profile(user=user, userinfo={"name": "test user"})

for permission in conduit_permissions:
user.user_permissions.add(permission)

user.save()
user.profile.save()

return user


@pytest.fixture
def authenticated_client(user, user_plaintext_password, client):
client.login(username=user.username, password=user_plaintext_password)
Expand Down
95 changes: 95 additions & 0 deletions src/lando/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import subprocess
import time

import pytest
import requests
from django.conf import settings
from django.contrib.auth.models import User

from lando.main.models import Profile


@pytest.fixture
def hg_clone(hg_server, tmpdir):
clone_dir = tmpdir.join("hg_clone")
subprocess.run(["hg", "clone", hg_server, clone_dir.strpath], check=True)
return clone_dir


@pytest.fixture
def hg_test_bundle():
return settings.BASE_DIR / "api" / "tests" / "data" / "test-repo.bundle"


@pytest.fixture
def hg_server(hg_test_bundle, tmpdir):
# TODO: Select open port.
port = "8000"
hg_url = "http://localhost:" + port

repo_dir = tmpdir.mkdir("hg_server")
subprocess.run(["hg", "clone", hg_test_bundle, repo_dir], check=True, cwd="/")

serve = subprocess.Popen(
[
"hg",
"serve",
"--config",
"web.push_ssl=False",
"--config",
"web.allow_push=*",
"-p",
port,
"-R",
repo_dir,
]
)
if serve.poll() is not None:
raise Exception("Failed to start the mercurial server.")
# Wait until the server is running.
for _i in range(10):
try:
requests.get(hg_url)
except Exception:
time.sleep(1)
break

yield hg_url
serve.kill()


@pytest.fixture
def conduit_permissions():
permissions = (
"scm_level_1",
"scm_level_2",
"scm_level_3",
"scm_conduit",
)
all_perms = Profile.get_all_scm_permissions()

return [all_perms[p] for p in permissions]


@pytest.fixture
def user_plaintext_password():
return "test_password"


@pytest.fixture
def user(user_plaintext_password, conduit_permissions):
user = User.objects.create_user(
username="test_user",
password=user_plaintext_password,
email="[email protected]",
)

user.profile = Profile(user=user, userinfo={"name": "test user"})

for permission in conduit_permissions:
user.user_permissions.add(permission)

user.save()
user.profile.save()

return user