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

add user, refactor tests #44

Merged
merged 2 commits into from
May 7, 2020
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
8 changes: 8 additions & 0 deletions pybuildkite/buildkite.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pybuildkite.annotations import Annotations
from pybuildkite.artifacts import Artifacts
from pybuildkite.teams import Teams
from pybuildkite.users import Users
from pybuildkite.decorators import requires_token


Expand Down Expand Up @@ -102,3 +103,10 @@ def teams(self):
Get Team operations for the Buildkite API
"""
return Teams(self.client, self.base_url)

@requires_token
def users(self):
"""
Get User operations for the Buildkite API
"""
return Users(self.client, self.base_url)
25 changes: 25 additions & 0 deletions pybuildkite/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pybuildkite.client import Client


class Users(Client):
"""
User operations for the Buildkite API
"""

def __init__(self, client: Client, base_url: str) -> None:
"""
Construct the class

:param client: API Client
:param base_url: Base Url
"""
self.client = client
self.path = base_url + "user"

def get_current_user(self):
"""
Returns the current user

:return: Returns current user
"""
return self.client.get(self.path)
26 changes: 6 additions & 20 deletions tests/test_agents.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
from unittest.mock import Mock

import pytest

from pybuildkite.agents import Agents


class TestAgents:
def test_get_agent(fake_client):
"""
Test functionality of the Agents class
Test the get_agent method
"""

@pytest.fixture
def fake_client(self):
"""
Build a fake API client
"""
return Mock(get=Mock())

def test_get_agent(self, fake_client):
"""
Test the get_agent method
"""
agents = Agents(fake_client, "base")
agents.get_agent("org_slug", "agent_id")
url = "base/organizations/org_slug/agents/agent_id"
fake_client.get.assert_called_with(url)
agents = Agents(fake_client, "base")
agents.get_agent("org_slug", "agent_id")
url = "base/organizations/org_slug/agents/agent_id"
fake_client.get.assert_called_with(url)
91 changes: 38 additions & 53 deletions tests/test_artifacts.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,43 @@
from unittest.mock import Mock

import pytest

from pybuildkite.artifacts import Artifacts


class TestArtifacts:
"""
Test functionality of the Jobs class
"""

@pytest.fixture
def fake_client(self):
"""
Build a fake API client
"""
return Mock(get=Mock())

def test_list_artifacts_for_build(self, fake_client):
"""
Test List Artifacts for build
"""
artifacts = Artifacts(fake_client, "base")
artifacts.list_artifacts_for_build("org_slug", "pipe_slug", "build_no")
url = (
"base/organizations/org_slug/pipelines/pipe_slug/builds/build_no/artifacts/"
)
fake_client.get.assert_called_with(url)

def test_list_artifacts_for_job(self, fake_client):
"""
Test list artifacts for job
"""
artifacts = Artifacts(fake_client, "base")
artifacts.list_artifacts_for_job("org_slug", "pipe_slug", "build_no", 123)
url = "base/organizations/org_slug/pipelines/pipe_slug/builds/build_no/jobs/123/artifacts/"
fake_client.get.assert_called_with(url)

def test_get_artifact(self, fake_client):
"""
Test get Artifact
"""
artifacts = Artifacts(fake_client, "base")
artifacts.get_artifact("org_slug", "pipe_slug", "build_no", 123, "artifact")
url = "base/organizations/org_slug/pipelines/pipe_slug/builds/build_no/jobs/123/artifacts/artifact/"
fake_client.get.assert_called_with(url)

def test_download_artifact(self, fake_client):
"""
Test download Artifact
"""
artifacts = Artifacts(fake_client, "base")
artifacts.download_artifact(
"org_slug", "pipe_slug", "build_no", 123, "artifact"
)
url = "base/organizations/org_slug/pipelines/pipe_slug/builds/build_no/jobs/123/artifacts/artifact/download/"
fake_client.get.assert_called_with(url)
def test_list_artifacts_for_build(fake_client):
"""
Test List Artifacts for build
"""
artifacts = Artifacts(fake_client, "base")
artifacts.list_artifacts_for_build("org_slug", "pipe_slug", "build_no")
url = "base/organizations/org_slug/pipelines/pipe_slug/builds/build_no/artifacts/"
fake_client.get.assert_called_with(url)


def test_list_artifacts_for_job(fake_client):
"""
Test list artifacts for job
"""
artifacts = Artifacts(fake_client, "base")
artifacts.list_artifacts_for_job("org_slug", "pipe_slug", "build_no", 123)
url = "base/organizations/org_slug/pipelines/pipe_slug/builds/build_no/jobs/123/artifacts/"
fake_client.get.assert_called_with(url)


def test_get_artifact(fake_client):
"""
Test get Artifact
"""
artifacts = Artifacts(fake_client, "base")
artifacts.get_artifact("org_slug", "pipe_slug", "build_no", 123, "artifact")
url = "base/organizations/org_slug/pipelines/pipe_slug/builds/build_no/jobs/123/artifacts/artifact/"
fake_client.get.assert_called_with(url)


def test_download_artifact(fake_client):
"""
Test download Artifact
"""
artifacts = Artifacts(fake_client, "base")
artifacts.download_artifact("org_slug", "pipe_slug", "build_no", 123, "artifact")
url = "base/organizations/org_slug/pipelines/pipe_slug/builds/build_no/jobs/123/artifacts/artifact/download/"
fake_client.get.assert_called_with(url)
28 changes: 12 additions & 16 deletions tests/test_buildkite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
from pybuildkite.exceptions import NoAcccessTokenException


class TestBuildkite:
def test_access_token_not_set_raises_exception():
"""
Test functionality of the Buildkite class
Test that exception is raised if no access token is set
"""
with pytest.raises(NoAcccessTokenException):
buildkite = Buildkite()
buildkite.agents()

def test_access_token_not_set_raises_exception(self):
"""
Test that exception is raised if no access token is set
"""
with pytest.raises(NoAcccessTokenException):
buildkite = Buildkite()
buildkite.agents()

def test_access_token_set(self):
"""
Test that methods can be called when access token is set
"""
buildkite = Buildkite()
buildkite.set_access_token("FAKE-ACCESS-TOKEN")
assert buildkite.agents()
def test_access_token_set():
"""
Test that methods can be called when access token is set
"""
buildkite = Buildkite()
buildkite.set_access_token("FAKE-ACCESS-TOKEN")
assert buildkite.agents()
55 changes: 22 additions & 33 deletions tests/test_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,28 @@
from pybuildkite.builds import Builds


class TestBuilds:
def test_meta_data_url(fake_client):
"""
Test functionality of the Builds class
Verifies if url is created properly when using meta_data
"""
meta_data = {"key1": 1, "key2": "2"}
builds = Builds(fake_client, "base")
builds.list_all(meta_data=meta_data)

@pytest.fixture
def fake_client(self):
"""
Build a fake API client
"""
return Mock(get=Mock())

def test_meta_data_url(self, fake_client):
"""
Verifies if url is created properly when using meta_data
"""
meta_data = {"key1": 1, "key2": "2"}
builds = Builds(fake_client, "base")
builds.list_all(meta_data=meta_data)

name, args, kwargs = fake_client.method_calls[-1]
_, query_params = args
assert query_params["meta_data[key1]"] == 1
assert query_params["meta_data[key2]"] == "2"

def test_no_meta_data_url(self, fake_client):
"""
Verifies if url is created properly when using meta_data
"""
builds = Builds(fake_client, "base")
builds.list_all()

name, args, kwargs = fake_client.method_calls[-1]
_, query_params = args
for key in query_params:
assert "meta_data" not in key
name, args, kwargs = fake_client.method_calls[-1]
_, query_params = args
assert query_params["meta_data[key1]"] == 1
assert query_params["meta_data[key2]"] == "2"


def test_no_meta_data_url(fake_client):
"""
Verifies if url is created properly when using meta_data
"""
builds = Builds(fake_client, "base")
builds.list_all()

name, args, kwargs = fake_client.method_calls[-1]
_, query_params = args
for key in query_params:
assert "meta_data" not in key
38 changes: 19 additions & 19 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ def test_request_should_not_include_token(self):
Test that the access token is not included in the call to
requests if it isn't actually set.
"""
client = Client()
fake_client = Client()

with patch("requests.request") as request:
request.return_value.json.return_value = {}

client.request("GET", "http://www.google.com/")
fake_client.request("GET", "http://www.google.com/")

request.assert_called_once_with(
"GET",
Expand All @@ -107,12 +107,12 @@ def test_request_preserves_accept_header(self):
"""
Test that the accept header is not overwritten and that text is returned
"""
client = Client()
fake_client = Client()

with patch("requests.request") as request:
request.return_value.text = "response text"

resp_text = client.request(
resp_text = fake_client.request(
"GET",
"http://www.google.com/",
headers={"Accept": "application/fake_encoding"},
Expand All @@ -129,47 +129,47 @@ def test_request_preserves_accept_header(self):

assert resp_text == "response text"

def test_request_should_include_token_when_set(self):
def test_request_should_return_json_when_no_header_provided(self):
"""
Test that the access token is not included in the call to
requests if it isn't actually set.
Test that the requests response.json() decoding is returned when
no accept header is provided
"""
client = Client()
client.set_client_access_token("ABCDEF1234")
fake_client = Client()

with patch("requests.request") as request:
request.return_value.json.return_value = {"key": "value"}

client.request("GET", "http://www.google.com/")
ret = fake_client.request("GET", "http://www.google.com/")

expected_params = b"per_page=100"
request.assert_called_once_with(
"GET",
"http://www.google.com/",
headers={"Authorization": "Bearer ABCDEF1234"},
headers={},
json=None,
params=expected_params,
)

def test_request_should_return_json_when_no_header_provided(self):
assert ret == {"key": "value"}

def test_request_should_include_token_when_set(self):
"""
Test that the requests response.json() decoding is returned when
no accept header is provided
Test that the access token is not included in the call to
requests if it isn't actually set.
"""
client = Client()
fake_client = Client()
fake_client.set_client_access_token("ABCDEF1234")

with patch("requests.request") as request:
request.return_value.json.return_value = {"key": "value"}

ret = client.request("GET", "http://www.google.com/")
ret = fake_client.request("GET", "http://www.google.com/")

expected_params = b"per_page=100"
request.assert_called_once_with(
"GET",
"http://www.google.com/",
headers={},
headers={"Authorization": "Bearer ABCDEF1234"},
json=None,
params=expected_params,
)

assert ret == {"key": "value"}
Loading