Пример #1
0
 def last_commit(self):
     """Returns a tuple (hash, and commit object)"""
     return git.get_last_commit(repo_path=self.path)
Пример #2
0
    def test_handle_new_files_task(self):
        assert self.model_class.objects.count() == 0
        user = self.auth_client.user
        repo_name = self.project.name
        uploaded_file = self.get_upload_file()

        self.auth_client.put(self.url,
                             data={'repo': uploaded_file},
                             content_type=MULTIPART_CONTENT)

        upload_file_path = '{}/{}/{}.tar.gz'.format(settings.UPLOAD_ROOT,
                                                    user.username, repo_name)
        # Assert the the task handler takes care of cleaning the upload root after
        # committing changes
        self.assertFalse(os.path.exists(upload_file_path))

        # Assert repo model was created
        assert self.model_class.objects.count() == 1
        repo = self.model_class.objects.first()

        # Assert new git repo was created in the repos root and that also the tar file was deleted
        code_file_path = '{}/{}/{}/{}'.format(settings.REPOS_ROOT,
                                              user.username, self.project.name,
                                              self.project.name)
        tar_code_file_path = repo.get_tmp_tar_path()
        self.assertFalse(os.path.exists(tar_code_file_path))
        self.assertTrue(os.path.exists(code_file_path))
        # Assert that the code_file_path is a git repo
        git_file_path = '{}/.git'.format(code_file_path)
        self.assertTrue(os.path.exists(git_file_path))
        # Get last commit
        commit_hash, commit = git.get_last_commit(code_file_path)
        assert commit.author.email == user.email
        assert commit.author.name == user.username

        # Make a new upload with repo_new.tar.gz containing 2 files
        new_uploaded_file = self.get_upload_file('updated_repo')
        self.auth_client.put(self.url,
                             data={'repo': new_uploaded_file},
                             content_type=MULTIPART_CONTENT)

        upload_file_path = '{}/{}/{}.tar.gz'.format(settings.UPLOAD_ROOT,
                                                    user.username, repo_name)
        # Assert the the task handler takes care of cleaning the upload root after
        # committing changes
        self.assertFalse(os.path.exists(upload_file_path))

        # Assert same git repo was used in the repos root and that also the tar file was deleted
        self.assertFalse(os.path.exists(tar_code_file_path))
        self.assertTrue(os.path.exists(code_file_path))
        # Assert that the code_file_path is a git repo
        self.assertTrue(os.path.exists(git_file_path))
        # Get last commit
        commit_hash, commit = git.get_last_commit(code_file_path)
        assert commit.author.email == user.email
        assert commit.author.name == user.username
        # Assert that we committed 3 files (2 files in new_repo.tar.gz one file was deleted)
        assert len(git.get_committed_files(code_file_path, commit_hash)) == 3

        # Make a new upload with repo_with_folder.tar.gz containing 1 file one dir with fil
        new_uploaded_file = self.get_upload_file('repo_with_folder')
        self.auth_client.put(self.url,
                             data={'repo': new_uploaded_file},
                             content_type=MULTIPART_CONTENT)

        upload_file_path = '{}/{}/{}.tar.gz'.format(settings.UPLOAD_ROOT,
                                                    user.username, repo_name)
        # Assert the the task handler takes care of cleaning the upload root after
        # committing changes
        self.assertFalse(os.path.exists(upload_file_path))

        # Assert same git repo was used in the repos root and that also the tar file was deleted
        self.assertFalse(os.path.exists(tar_code_file_path))
        self.assertTrue(os.path.exists(code_file_path))
        # Assert that the code_file_path is a git repo
        self.assertTrue(os.path.exists(git_file_path))
        # Get last commit
        commit_hash, commit = git.get_last_commit(code_file_path)
        assert commit.author.email == user.email
        assert commit.author.name == user.username
        # Assert that we committed 3 files
        # (1 file updated 1 deleted, and one folder with 1 file added)
        assert len(git.get_committed_files(code_file_path, commit_hash)) == 3

        # Check that other user cannot commit to this repo
        new_user = UserFactory()
        self.auth_client.login_user(new_user)
        new_uploaded_file = self.get_upload_file('updated_repo')
        response = self.auth_client.put(self.url,
                                        data={'repo': new_uploaded_file},
                                        content_type=MULTIPART_CONTENT)

        assert response.status_code in (status.HTTP_401_UNAUTHORIZED,
                                        status.HTTP_403_FORBIDDEN)
        upload_file_path = '{}/{}/{}.tar.gz'.format(settings.UPLOAD_ROOT,
                                                    new_user.username,
                                                    repo_name)
        # Assert the the task handler takes care of cleaning the upload root after
        # committing changes
        self.assertFalse(os.path.exists(upload_file_path))

        # Assert same git repo was used in the repos root and that also the tar file was deleted
        self.assertFalse(os.path.exists(tar_code_file_path))
        self.assertTrue(os.path.exists(code_file_path))
        # Assert that the code_file_path is a git repo
        self.assertTrue(os.path.exists(git_file_path))
        # Get last commit and check it did not change
        new_commit_hash, new_commit = git.get_last_commit(code_file_path)
        assert commit.author.email == new_commit.author.email
        assert commit.author.name == new_commit.author.name
        assert new_commit_hash == commit_hash

        # Log old user, otherwise other tests will crash
        self.auth_client.login_user(user)