|
| 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 | + "tmpl, tmpl_mgr, tmpl_path", |
| 29 | + [ |
| 30 | + (Dockerfile, "dockerfiles", "dockerfiles"), |
| 31 | + (Gitignore, "gitignores", "gitignores"), |
| 32 | + (Gitlabciyml, "gitlabciymls", "gitlab_ci_ymls"), |
| 33 | + (License, "licenses", "licenses"), |
| 34 | + ], |
| 35 | + ids=[ |
| 36 | + "dockerfile", |
| 37 | + "gitignore", |
| 38 | + "gitlabciyml", |
| 39 | + "license", |
| 40 | + ], |
| 41 | +) |
| 42 | +def test_get_template(gl, tmpl, tmpl_mgr, tmpl_path): |
| 43 | + tmpl_id = "sample" |
| 44 | + tmpl_content = {"name": tmpl_id, "content": "Sample template content"} |
| 45 | + |
| 46 | + # License templates have 'key' as the id attribute, so ensure |
| 47 | + # this is included in the response content |
| 48 | + if tmpl == License: |
| 49 | + tmpl_id = "smpl" |
| 50 | + tmpl_content.update({"key": tmpl_id}) |
| 51 | + |
| 52 | + path = f"templates/{tmpl_path}/{tmpl_id}" |
| 53 | + with responses.RequestsMock() as rsps: |
| 54 | + rsps.add( |
| 55 | + method=responses.GET, |
| 56 | + url=f"http://localhost/api/v4/{path}", |
| 57 | + json=tmpl_content, |
| 58 | + ) |
| 59 | + |
| 60 | + template = getattr(gl, tmpl_mgr).get(tmpl_id) |
| 61 | + |
| 62 | + assert isinstance(template, tmpl) |
| 63 | + assert getattr(template, template._id_attr) == tmpl_id |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize( |
| 67 | + "tmpl, tmpl_mgr, tmpl_path", |
| 68 | + [ |
| 69 | + (ProjectDockerfileTemplate, "dockerfile_templates", "dockerfiles"), |
| 70 | + (ProjectGitignoreTemplate, "gitignore_templates", "gitignores"), |
| 71 | + (ProjectGitlabciymlTemplate, "gitlabciyml_templates", "gitlab_ci_ymls"), |
| 72 | + (ProjectLicenseTemplate, "license_templates", "licenses"), |
| 73 | + (ProjectIssueTemplate, "issue_templates", "issues"), |
| 74 | + (ProjectMergeRequestTemplate, "merge_request_templates", "merge_requests"), |
| 75 | + ], |
| 76 | + ids=[ |
| 77 | + "dockerfile", |
| 78 | + "gitignore", |
| 79 | + "gitlabciyml", |
| 80 | + "license", |
| 81 | + "issue", |
| 82 | + "mergerequest", |
| 83 | + ], |
| 84 | +) |
| 85 | +def test_get_project_template(project, tmpl, tmpl_mgr, tmpl_path): |
| 86 | + tmpl_id = "sample" |
| 87 | + tmpl_content = {"name": tmpl_id, "content": "Sample template content"} |
| 88 | + |
| 89 | + # ProjectLicenseTemplate templates have 'key' as the id attribute, so ensure |
| 90 | + # this is included in the response content |
| 91 | + if tmpl == ProjectLicenseTemplate: |
| 92 | + tmpl_id = "smpl" |
| 93 | + tmpl_content.update({"key": tmpl_id}) |
| 94 | + |
| 95 | + path = f"projects/{project.id}/templates/{tmpl_path}/{tmpl_id}" |
| 96 | + with responses.RequestsMock() as rsps: |
| 97 | + rsps.add( |
| 98 | + method=responses.GET, |
| 99 | + url=f"http://localhost/api/v4/{path}", |
| 100 | + json=tmpl_content, |
| 101 | + ) |
| 102 | + |
| 103 | + template = getattr(project, tmpl_mgr).get(tmpl_id) |
| 104 | + |
| 105 | + assert isinstance(template, tmpl) |
| 106 | + assert getattr(template, template._id_attr) == tmpl_id |
0 commit comments