Skip to content

Commit

Permalink
tests: Remove classes from tests (endpoints)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Jul 19, 2024
1 parent c973125 commit 49225e6
Showing 1 changed file with 58 additions and 51 deletions.
109 changes: 58 additions & 51 deletions tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,75 +29,82 @@ def quotesbot_egg_asyncio():
yield egg


class TestEndpoint:
def test_urljoin(self, mock_scrapyd):
assert mock_scrapyd.urljoin("foo") == mock_scrapyd.url + "foo"
def _deploy(mock_scrapyd, quotesbot_egg) -> Response:
url = mock_scrapyd.urljoin("addversion.json")
data = {b"project": b"quotesbot", b"version": b"0.01"}
files = {b"egg": quotesbot_egg}
return requests.post(url, data=data, files=files)

def test_root(self, mock_scrapyd):
resp = requests.get(mock_scrapyd.url)

assert resp.status_code == 200
assert re.search("To schedule a spider you need to use the API", resp.text)
def test_urljoin(mock_scrapyd):
assert mock_scrapyd.urljoin("foo") == mock_scrapyd.url + "foo"

def test_auth(self):
username, password = "Leonardo", "hunter2"

with MockScrapydServer(authentication=username + ":" + password) as server:
assert requests.get(server.url).status_code == 401
def test_root(mock_scrapyd):
resp = requests.get(mock_scrapyd.url)

res = requests.get(server.url, auth=(username, password))
assert resp.status_code == 200
assert re.search("To schedule a spider you need to use the API", resp.text)

assert res.status_code == 200
assert re.search("To schedule a spider", res.text)

res = requests.get(server.url, auth=(username, "trying to hack"))
def test_auth():
username, password = "Leonardo", "hunter2"

assert res.status_code == 401
with MockScrapydServer(authentication=username + ":" + password) as server:
assert requests.get(server.url).status_code == 401

def test_launch_spider_get(self, mock_scrapyd):
resp = requests.get(mock_scrapyd.urljoin("schedule.json"))
res = requests.get(server.url, auth=(username, password))

assert resp.status_code == 200
assert resp.json()["status"] == "error"
assert res.status_code == 200
assert re.search("To schedule a spider", res.text)

def test_spider_list_no_project(self, mock_scrapyd):
resp = requests.get(mock_scrapyd.urljoin("listspiders.json"))
data = resp.json()
res = requests.get(server.url, auth=(username, "trying to hack"))

assert resp.status_code == 200
assert data["status"] == "error"
assert data["message"] == "'project' parameter is required"
assert res.status_code == 401

def test_spider_list_project_no_egg(self, mock_scrapyd):
resp = requests.get(mock_scrapyd.urljoin("listprojects.json"))
data = resp.json()

assert resp.status_code == 200
assert data["status"] == "ok"
def test_launch_spider_get(mock_scrapyd):
resp = requests.get(mock_scrapyd.urljoin("schedule.json"))

def test_addversion_and_delversion(self, mock_scrapyd, quotesbot_egg):
resp = self._deploy(mock_scrapyd, quotesbot_egg)
data = resp.json()
assert resp.status_code == 200
assert resp.json()["status"] == "error"

assert resp.status_code == 200
assert data["spiders"] == 2
assert data["status"] == "ok"
assert data["project"] == "quotesbot"

url = mock_scrapyd.urljoin("delversion.json")
res = requests.post(url, data={"project": "quotesbot", "version": "0.01"})
def test_spider_list_no_project(mock_scrapyd):
resp = requests.get(mock_scrapyd.urljoin("listspiders.json"))
data = resp.json()

assert res.status_code == 200
assert res.json()["status"] == "ok"
assert resp.status_code == 200
assert data["status"] == "error"
assert data["message"] == "'project' parameter is required"


def test_spider_list_project_no_egg(mock_scrapyd):
resp = requests.get(mock_scrapyd.urljoin("listprojects.json"))
data = resp.json()

assert resp.status_code == 200
assert data["status"] == "ok"


def test_addversion_and_delversion(mock_scrapyd, quotesbot_egg):
resp = _deploy(mock_scrapyd, quotesbot_egg)
data = resp.json()

assert resp.status_code == 200
assert data["spiders"] == 2
assert data["status"] == "ok"
assert data["project"] == "quotesbot"

url = mock_scrapyd.urljoin("delversion.json")
res = requests.post(url, data={"project": "quotesbot", "version": "0.01"})

assert res.status_code == 200
assert res.json()["status"] == "ok"

def _deploy(self, mock_scrapyd, quotesbot_egg) -> Response:
url = mock_scrapyd.urljoin("addversion.json")
data = {b"project": b"quotesbot", b"version": b"0.01"}
files = {b"egg": quotesbot_egg}
return requests.post(url, data=data, files=files)

def test_failed_settings(self, mock_scrapyd, quotesbot_egg_asyncio):
response = self._deploy(mock_scrapyd, quotesbot_egg_asyncio)
def test_failed_settings(mock_scrapyd, quotesbot_egg_asyncio):
response = _deploy(mock_scrapyd, quotesbot_egg_asyncio)

assert response.status_code == 200
assert response.json()["status"] == "ok"
assert response.status_code == 200
assert response.json()["status"] == "ok"

0 comments on commit 49225e6

Please sign in to comment.