Skip to content

test: update tests and params for project export/import #1040

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

Merged
merged 8 commits into from
Mar 19, 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
161 changes: 156 additions & 5 deletions gitlab/tests/objects/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,129 @@
import requests
from gitlab import * # noqa
from gitlab.v4.objects import * # noqa
from httmock import HTTMock, urlmatch, response # noqa
from httmock import HTTMock, urlmatch, response, with_httmock # noqa


headers = {"content-type": "application/json"}
binary_content = b"binary content"


class TestProjectSnippets(unittest.TestCase):
@urlmatch(
scheme="http", netloc="localhost", path="/api/v4/projects/1/export", method="post",
)
def resp_create_export(url, request):
"""Common mock for Project Export tests."""
content = """{
"message": "202 Accepted"
}"""
content = content.encode("utf-8")
return response(202, content, headers, None, 25, request)


@urlmatch(
scheme="http", netloc="localhost", path="/api/v4/projects/1/export", method="get",
)
def resp_export_status(url, request):
"""Mock for Project Export GET response."""
content = """{
"id": 1,
"description": "Itaque perspiciatis minima aspernatur",
"name": "Gitlab Test",
"name_with_namespace": "Gitlab Org / Gitlab Test",
"path": "gitlab-test",
"path_with_namespace": "gitlab-org/gitlab-test",
"created_at": "2017-08-29T04:36:44.383Z",
"export_status": "finished",
"_links": {
"api_url": "https://gitlab.test/api/v4/projects/1/export/download",
"web_url": "https://gitlab.test/gitlab-test/download_export"
}
}
"""
content = content.encode("utf-8")
return response(200, content, headers, None, 25, request)


@urlmatch(
scheme="http",
netloc="localhost",
path="/api/v4/projects/1/export/download",
method="get",
)
def resp_download_export(url, request):
"""Mock for Project Export Download GET response."""
headers = {"content-type": "application/octet-stream"}
content = binary_content
return response(200, content, headers, None, 25, request)


@urlmatch(
scheme="http", netloc="localhost", path="/api/v4/projects/import", method="post",
)
def resp_import_project(url, request):
"""Mock for Project Import POST response."""
content = """{
"id": 1,
"description": null,
"name": "api-project",
"name_with_namespace": "Administrator / api-project",
"path": "api-project",
"path_with_namespace": "root/api-project",
"created_at": "2018-02-13T09:05:58.023Z",
"import_status": "scheduled"
}"""
content = content.encode("utf-8")
return response(200, content, headers, None, 25, request)


@urlmatch(
scheme="http", netloc="localhost", path="/api/v4/projects/1/import", method="get",
)
def resp_import_status(url, request):
"""Mock for Project Import GET response."""
content = """{
"id": 1,
"description": "Itaque perspiciatis minima aspernatur corporis consequatur.",
"name": "Gitlab Test",
"name_with_namespace": "Gitlab Org / Gitlab Test",
"path": "gitlab-test",
"path_with_namespace": "gitlab-org/gitlab-test",
"created_at": "2017-08-29T04:36:44.383Z",
"import_status": "finished"
}"""
content = content.encode("utf-8")
return response(200, content, headers, None, 25, request)


@urlmatch(
scheme="http", netloc="localhost", path="/api/v4/import/github", method="post",
)
def resp_import_github(url, request):
"""Mock for GitHub Project Import POST response."""
content = """{
"id": 27,
"name": "my-repo",
"full_path": "/root/my-repo",
"full_name": "Administrator / my-repo"
}"""
content = content.encode("utf-8")
return response(200, content, headers, None, 25, request)


class TestProject(unittest.TestCase):
"""Base class for GitLab Project tests."""

def setUp(self):
self.gl = Gitlab(
"http://localhost",
private_token="private_token",
ssl_verify=True,
api_version=4,
)
self.project = self.gl.projects.get(1, lazy=True)


class TestProjectSnippets(TestProject):
def test_list_project_snippets(self):
title = "Example Snippet Title"
visibility = "private"
Expand All @@ -47,7 +155,7 @@ def resp_list_snippet(url, request):
return response(200, content, headers, None, 25, request)

with HTTMock(resp_list_snippet):
snippets = self.gl.projects.get(1, lazy=True).snippets.list()
snippets = self.project.snippets.list()
self.assertEqual(len(snippets), 1)
self.assertEqual(snippets[0].title, title)
self.assertEqual(snippets[0].visibility, visibility)
Expand Down Expand Up @@ -76,7 +184,7 @@ def resp_get_snippet(url, request):
return response(200, content, headers, None, 25, request)

with HTTMock(resp_get_snippet):
snippet = self.gl.projects.get(1, lazy=True).snippets.get(1)
snippet = self.project.snippets.get(1)
self.assertEqual(snippet.title, title)
self.assertEqual(snippet.visibility, visibility)

Expand Down Expand Up @@ -123,7 +231,7 @@ def resp_create_snippet(url, request):
return response(200, content, headers, None, 25, request)

with HTTMock(resp_create_snippet, resp_update_snippet):
snippet = self.gl.projects.get(1, lazy=True).snippets.create(
snippet = self.project.snippets.create(
{
"title": title,
"file_name": title,
Expand All @@ -138,3 +246,46 @@ def resp_create_snippet(url, request):
snippet.save()
self.assertEqual(snippet.title, title)
self.assertEqual(snippet.visibility, visibility)


class TestProjectExport(TestProject):
@with_httmock(resp_create_export)
def test_create_project_export(self):
export = self.project.exports.create()
self.assertEqual(export.message, "202 Accepted")

@with_httmock(resp_create_export, resp_export_status)
def test_refresh_project_export_status(self):
export = self.project.exports.create()
export.refresh()
self.assertEqual(export.export_status, "finished")

@with_httmock(resp_create_export, resp_download_export)
def test_download_project_export(self):
export = self.project.exports.create()
download = export.download()
self.assertIsInstance(download, bytes)
self.assertEqual(download, binary_content)


class TestProjectImport(TestProject):
@with_httmock(resp_import_project)
def test_import_project(self):
project_import = self.gl.projects.import_project("file", "api-project")
self.assertEqual(project_import["import_status"], "scheduled")

@with_httmock(resp_import_status)
def test_refresh_project_import_status(self):
project_import = self.project.imports.get()
project_import.refresh()
self.assertEqual(project_import.import_status, "finished")

@with_httmock(resp_import_github)
def test_import_github(self):
base_path = "/root"
name = "my-repo"
ret = self.gl.projects.import_github("githubkey", 1234, base_path, name)
self.assertIsInstance(ret, dict)
self.assertEqual(ret["name"], name)
self.assertEqual(ret["full_path"], "/".join((base_path, name)))
self.assertTrue(ret["full_name"].endswith(name))
27 changes: 0 additions & 27 deletions gitlab/tests/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,33 +937,6 @@ def resp_update_submodule(url, request):
self.assertEqual(ret["message"], "Message")
self.assertEqual(ret["id"], "ed899a2f4b50b4370feeea94676502b42383c746")

def test_import_github(self):
@urlmatch(
scheme="http",
netloc="localhost",
path="/api/v4/import/github",
method="post",
)
def resp_import_github(url, request):
headers = {"content-type": "application/json"}
content = """{
"id": 27,
"name": "my-repo",
"full_path": "/root/my-repo",
"full_name": "Administrator / my-repo"
}"""
content = content.encode("utf-8")
return response(200, content, headers, None, 25, request)

with HTTMock(resp_import_github):
base_path = "/root"
name = "my-repo"
ret = self.gl.projects.import_github("githubkey", 1234, base_path, name)
self.assertIsInstance(ret, dict)
self.assertEqual(ret["name"], name)
self.assertEqual(ret["full_path"], "/".join((base_path, name)))
self.assertTrue(ret["full_name"].endswith(name))

def test_applications(self):
content = '{"name": "test_app", "redirect_uri": "http://localhost:8080", "scopes": ["api", "email"]}'
json_content = json.loads(content)
Expand Down
3 changes: 3 additions & 0 deletions gitlab/v4/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4839,6 +4839,7 @@ def import_project(
self,
file,
path,
name=None,
namespace=None,
overwrite=False,
override_params=None,
Expand Down Expand Up @@ -4868,6 +4869,8 @@ def import_project(
if override_params:
for k, v in override_params.items():
data["override_params[%s]" % k] = v
if name is not None:
data["name"] = name
if namespace:
data["namespace"] = namespace
return self.gitlab.http_post(
Expand Down
6 changes: 5 additions & 1 deletion tools/python_test_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,9 +962,13 @@
ex.download(streamed=True, action=f.write)

output = gl.projects.import_project(
open("/tmp/gitlab-export.tgz", "rb"), "imported_project"
open("/tmp/gitlab-export.tgz", "rb"), "imported_project", name="Imported Project"
)
project_import = gl.projects.get(output["id"], lazy=True).imports.get()

assert project_import.path == "imported_project"
assert project_import.name == "Imported Project"

count = 0
while project_import.import_status != "finished":
time.sleep(1)
Expand Down