#!/usr/bin/python3

import unittest
from unittest.mock import patch
import sys
import os
import git
import importlib.machinery

# Import the hook file as a module
weblate_hook = importlib.machinery.SourceFileLoader('weblate_hook', 'tails-weblate-update.hook').load_module()
main = weblate_hook.main


class TestWeblateUpdate(unittest.TestCase):
    @patch.object(sys, 'argv', ['hooks/update', 'refs/heads/master',
                                '03b89c8f4b55306818f19f610cababbadcdc54c0',
                                '0000000000000000000000000000000000000000'])
    def test_deleting_branch(self):
        with patch.object(os, 'environ', {'GL_USER': ''}):
            self.assertIsNone(main())
        with patch.object(os, 'environ', {'GL_USER': weblate_hook.EXPECTED_GL_USER}):
            with self.assertRaises(SystemExit) as e:
                main()
            self.assertEqual(e.exception.args,
                             ('[POLICY] Weblate is not allowed to delete branches.',))

    @patch.object(sys, 'argv', ['hooks/update', 'refs/heads/master',
                                '0000000000000000000000000000000000000000',
                                '03b89c8f4b55306818f19f610cababbadcdc54c0'])
    def test_creating_branch(self):
        with patch.object(os, 'environ', {'GL_USER': ''}):
            self.assertIsNone(main())
        with patch.object(os, 'environ', {'GL_USER': weblate_hook.EXPECTED_GL_USER}):
            with self.assertRaises(SystemExit) as e:
                main()
            self.assertEqual(e.exception.args,
                             ('[POLICY] Weblate is not allowed to create new branches.',))

    @patch.object(sys, 'argv', ['hooks/update', 'refs/heads/master',
                                'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
                                'a3b89c8f4b553a6818f19f61acababbadcdc54ca'])
    def test_call_check_commit_author_weblate(self):
        with patch('weblate_hook.check_commit_author_weblate', return_value=None) as func:
            with patch.object(os, 'environ', {'GL_USER': ''}):
                self.assertIsNone(main())
                func.assert_not_called()
            with patch.object(os, 'environ', {'GL_USER': weblate_hook.EXPECTED_GL_USER}):
                self.assertIsNone(main())
                func.assert_called_once_with(git.Repo(''),
                                             'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
                                             'a3b89c8f4b553a6818f19f61acababbadcdc54ca')

    @patch.object(sys, 'argv', ['hooks/update', 'refs/heads/master',
                                'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
                                'a3b89c8f4b553a6818f19f61acababbadcdc54ca'])
    def test_failing_call_check_commit_author_weblate(self):
        with patch('weblate_hook.check_commit_author_weblate', return_value='foo') as func:
            with patch.object(os, 'environ', {'GL_USER': ''}):
                self.assertIsNone(main())
                func.assert_not_called()
            with patch.object(os, 'environ', {'GL_USER': weblate_hook.EXPECTED_GL_USER}):
                with self.assertRaises(SystemExit) as e:
                    main()
                self.assertEqual(e.exception.args, ('foo',))
                func.assert_called_once_with(git.Repo(''),
                                             'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
                                             'a3b89c8f4b553a6818f19f61acababbadcdc54ca')


if __name__ == '__main__':
    unittest.main()

