|
| 1 | +""" |
| 2 | +Gitlab API: |
| 3 | +https://docs.gitlab.com/ce/api/templates/dockerfiles.html |
| 4 | +https://docs.gitlab.com/ce/api/templates/gitignores.html |
| 5 | +https://docs.gitlab.com/ce/api/templates/gitlab_ci_ymls.html |
| 6 | +https://docs.gitlab.com/ce/api/templates/licenses.html |
| 7 | +https://docs.gitlab.com/ce/api/project_templates.html |
| 8 | +""" |
| 9 | + |
| 10 | +import pytest |
| 11 | +import responses |
| 12 | + |
| 13 | +from gitlab.v4.objects import ( |
| 14 | + Dockerfile, |
| 15 | + Gitignore, |
| 16 | + Gitlabciyml, |
| 17 | + License, |
| 18 | + ProjectDockerfileTemplate, |
| 19 | + ProjectGitignoreTemplate, |
| 20 | + ProjectGitlabciymlTemplate, |
| 21 | + ProjectIssueTemplate, |
| 22 | + ProjectLicenseTemplate, |
| 23 | + ProjectMergeRequestTemplate, |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize( |
| 28 | + "fixture, tmpl, tmpl_path, project_tmpl_mgr", |
| 29 | + [ |
| 30 | + ("gl", Dockerfile, "dockerfiles", "dockerfiles"), |
| 31 | + ("gl", Gitignore, "gitignores", "gitignores"), |
| 32 | + ("gl", Gitlabciyml, "gitlab_ci_ymls", "gitlabciymls"), |
| 33 | + ("gl", License, "licenses", "licenses"), |
| 34 | + ("project", ProjectDockerfileTemplate, "dockerfiles", "dockerfile_templates"), |
| 35 | + ("project", ProjectGitignoreTemplate, "gitignores", "gitignore_templates"), |
| 36 | + ( |
| 37 | + "project", |
| 38 | + ProjectGitlabciymlTemplate, |
| 39 | + "gitlab_ci_ymls", |
| 40 | + "gitlabciyml_templates", |
| 41 | + ), |
| 42 | + ("project", ProjectLicenseTemplate, "licenses", "license_templates"), |
| 43 | + ("project", ProjectIssueTemplate, "issues", "issue_templates"), |
| 44 | + ( |
| 45 | + "project", |
| 46 | + ProjectMergeRequestTemplate, |
| 47 | + "merge_requests", |
| 48 | + "mergerequest_templates", |
| 49 | + ), |
| 50 | + ], |
| 51 | + ids=[ |
| 52 | + "dockerfile", |
| 53 | + "gitignore", |
| 54 | + "gitlabciyml", |
| 55 | + "license", |
| 56 | + "project_dockerfile", |
| 57 | + "project_gitignore", |
| 58 | + "project_gitlabciyml", |
| 59 | + "project_license", |
| 60 | + "project_issue", |
| 61 | + "project_mergerequest", |
| 62 | + ], |
| 63 | +) |
| 64 | +def test_get_template(request, fixture, tmpl, tmpl_path, project_tmpl_mgr): |
| 65 | + # Get the appropriate fixture and path. `owner` is the object that |
| 66 | + # aggregates the template managers, i.e., Gitlab, Project |
| 67 | + owner = request.getfixturevalue(fixture) |
| 68 | + owner_path = "/" if fixture == "gl" else f"/projects/{owner.id}/" |
| 69 | + |
| 70 | + tmpl_id = "sample" |
| 71 | + tmpl_content = {"name": tmpl_id, "content": "Sample template content"} |
| 72 | + |
| 73 | + # License templates have 'key' as the id attribute, so ensure |
| 74 | + # this is included in the response content |
| 75 | + if tmpl == License or tmpl == ProjectLicenseTemplate: |
| 76 | + tmpl_id = "smpl" |
| 77 | + tmpl_content.update({"key": tmpl_id}) |
| 78 | + |
| 79 | + with responses.RequestsMock() as rsps: |
| 80 | + rsps.add( |
| 81 | + method=responses.GET, |
| 82 | + url=f"http://localhost/api/v4{owner_path}templates/{tmpl_path}/{tmpl_id}", |
| 83 | + json=tmpl_content, |
| 84 | + ) |
| 85 | + |
| 86 | + template = getattr(owner, project_tmpl_mgr).get(tmpl_id) |
| 87 | + |
| 88 | + assert isinstance(template, tmpl) |
| 89 | + assert getattr(template, template._id_attr) == tmpl_id |
0 commit comments