123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- # -*- coding: utf-8 -*-
- """
- (c) 2020 - Copyright Red Hat Inc
- Authors:
- Pierre-Yves Chibon <pingou@pingoured.fr>
- """
- from __future__ import unicode_literals, absolute_import
- import datetime
- import json
- import unittest
- import shutil
- import sys
- import tempfile
- import os
- import pygit2
- from celery.result import EagerResult
- from mock import patch, Mock
- sys.path.insert(
- 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
- )
- import pagure.flask_app
- import pagure.lib.query
- import tests
- from pagure.lib.repo import PagureRepo
- class PagureFlaskApiProjectViewFiletests(tests.Modeltests):
- """Tests for the flask API of pagure for issue"""
- maxDiff = None
- def setUp(self):
- super(PagureFlaskApiProjectViewFiletests, self).setUp()
- tests.create_projects(self.session)
- tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
- tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
- def test_view_file_invalid_project(self):
- output = self.app.get("/api/0/invalid/tree")
- self.assertEqual(output.status_code, 404)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data, {"error": "Project not found", "error_code": "ENOPROJECT"}
- )
- def test_view_file_invalid_ref_and_path(self):
- output = self.app.get("/api/0/test/tree/branchname/f/foldername")
- self.assertEqual(output.status_code, 404)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data,
- {
- "error": "File not found in this git repository",
- "error_code": "EFILENOTFOUND",
- },
- )
- def test_view_file_empty_project(self):
- output = self.app.get("/api/0/test2/tree")
- self.assertEqual(output.status_code, 404)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data,
- {
- "error": "This git repository is empty",
- "error_code": "EEMPTYGIT",
- },
- )
- def test_view_file_basic(self):
- output = self.app.get("/api/0/test/tree")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data,
- {
- "content": [
- {
- "content_url": "http://localhost/test/raw/master/"
- "f/README.rst",
- "name": "README.rst",
- "path": "README.rst",
- "type": "file",
- }
- ],
- "name": None,
- "type": "folder",
- },
- )
- def test_view_file_with_folder(self):
- tests.add_content_git_repo(
- os.path.join(self.path, "repos", "test.git")
- )
- output = self.app.get("/api/0/test/tree")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data,
- {
- "content": [
- {
- "content_url": "http://localhost/api/0/test/tree/"
- "master/f/folder1",
- "name": "folder1",
- "path": "folder1",
- "type": "folder",
- },
- {
- "content_url": "http://localhost/test/raw/master/f/"
- "README.rst",
- "name": "README.rst",
- "path": "README.rst",
- "type": "file",
- },
- {
- "content_url": "http://localhost/test/raw/master/f/"
- "sources",
- "name": "sources",
- "path": "sources",
- "type": "file",
- },
- ],
- "name": None,
- "type": "folder",
- },
- )
- def test_view_file_specific_file(self):
- tests.add_content_git_repo(
- os.path.join(self.path, "repos", "test.git")
- )
- output = self.app.get("/api/0/test/tree/master/f/README.rst")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data,
- {
- "content_url": "http://localhost/test/raw/master/f/README.rst",
- "name": "README.rst",
- "type": "file",
- },
- )
- def test_view_file_invalid_ref(self):
- tests.add_content_git_repo(
- os.path.join(self.path, "repos", "test.git")
- )
- output = self.app.get("/api/0/test/tree/invalid/f/folder1")
- print(output.data)
- self.assertEqual(output.status_code, 404)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data,
- {
- "error": "File not found in this git repository",
- "error_code": "EFILENOTFOUND",
- },
- )
- def test_view_file_invalid_folder(self):
- tests.add_content_git_repo(
- os.path.join(self.path, "repos", "test.git")
- )
- output = self.app.get("/api/0/test/tree/master/f/inv/invalid")
- self.assertEqual(output.status_code, 404)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data,
- {
- "error": "File not found in this git repository",
- "error_code": "EFILENOTFOUND",
- },
- )
- def test_view_file_valid_branch(self):
- tests.add_content_git_repo(
- os.path.join(self.path, "repos", "test.git")
- )
- output = self.app.get("/api/0/test/tree/master/f/folder1")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data,
- {
- "content": [
- {
- "content_url": "http://localhost/api/0/test/tree/"
- "master/f/folder1/folder2",
- "name": "folder2",
- "path": "folder1/folder2",
- "type": "folder",
- }
- ],
- "name": "folder1",
- "type": "folder",
- },
- )
- def test_view_file_non_ascii_name(self):
- # View file with a non-ascii name
- tests.add_commit_git_repo(
- os.path.join(self.path, "repos", "test.git"),
- ncommits=1,
- filename="Šource",
- )
- output = self.app.get("/api/0/test/tree")
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True).encode("utf-8"))
- self.assertDictEqual(
- data,
- {
- "content": [
- {
- "content_url": "http://localhost/test/raw/master/f/"
- "README.rst",
- "name": "README.rst",
- "path": "README.rst",
- "type": "file",
- },
- {
- "content_url": "http://localhost/test/raw/master/f/%C5%A0ource",
- "name": "Šource",
- "path": "Šource",
- "type": "file",
- },
- ],
- "name": None,
- "type": "folder",
- },
- )
- def test_view_file_from_commit(self):
- repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
- commit = repo.revparse_single("HEAD")
- output = self.app.get("/api/0/test/tree/%s" % commit.oid.hex)
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data,
- {
- "content": [
- {
- "content_url": "http://localhost/test/raw/"
- "%s/f/README.rst" % commit.oid.hex,
- "name": "README.rst",
- "path": "README.rst",
- "type": "file",
- }
- ],
- "name": None,
- "type": "folder",
- },
- )
- def test_view_file_from_tree(self):
- tests.add_content_git_repo(
- os.path.join(self.path, "repos", "test.git")
- )
- repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
- commit = repo.revparse_single("HEAD")
- output = self.app.get(
- "/api/0/test/tree/%s/f/folder1" % commit.tree.oid.hex
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data,
- {
- "content": [
- {
- "content_url": "http://localhost/api/0/test/tree/"
- "%s/f/folder1/folder2" % commit.tree.oid.hex,
- "name": "folder2",
- "path": "folder1/folder2",
- "type": "folder",
- }
- ],
- "name": "folder1",
- "type": "folder",
- },
- )
- def test_view_file_from_tag_hex(self):
- repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
- commit = repo.revparse_single("HEAD")
- tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0)
- tag = repo.create_tag(
- "v1.0_tag",
- commit.oid.hex,
- pygit2.GIT_OBJ_COMMIT,
- tagger,
- "Release v1.0",
- )
- output = self.app.get("/api/0/test/tree/%s" % tag.hex)
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(
- data,
- {
- "content": [
- {
- "content_url": "http://localhost/test/raw/"
- "%s/f/README.rst" % tag.hex,
- "name": "README.rst",
- "path": "README.rst",
- "type": "file",
- }
- ],
- "name": None,
- "type": "folder",
- },
- )
- if __name__ == "__main__":
- unittest.main(verbosity=2)
|