def test_release_rasies_exception_if_response_doesnt_contain_id(self, mock_GitHubGateway):
        expected_data = '{"valid": "json"}'
        mock_GitHubGateway.return_value.post_json_data.return_value = self.get_mock_response(code=201, data=expected_data)
        expected_error = 'Failed to create release: Got response code: {}  response: {}'.format(201, expected_data)

        ghr = GitHubRelease(**self.kwargs)
        with self.assertRaises(Exception) as context:
            ghr.release()

        self.assertEqual(expected_error, context.exception.message)
    def test_release_rasies_exception_if_response_code_is_not_201(self, mock_GitHubGateway):
        expected_data = "404 happens"
        mock_GitHubGateway.return_value.post_json_data.return_value = self.get_mock_response(code=404, data=expected_data)
        expected_error = 'Failed to create release: Got response code: {}  error: {}'.format(404, expected_data)

        ghr = GitHubRelease(**self.kwargs)
        with self.assertRaises(Exception) as context:
            ghr.release()

        self.assertEqual(expected_error, context.exception.message)
    def test_release_raises_exception_if_upload_fails(self, mock_GitHubGateway):
        self.setup_mock(mock_GitHubGateway)
        self.kwargs['files'] = self.files
        mock_GitHubGateway.return_value.post_file.return_value = self.get_mock_response(code=404, data='404 File not found')

        mock_open_file = mock_open()

        with patch('github_release.open', mock_open_file, create=True):
            with self.assertRaises(Exception) as context:
                ghr = GitHubRelease(**self.kwargs)
                ghr.release()

        self.assertEqual('Failed to upload assets: Got response code: 404  error: 404 File not found', context.exception.message)
    def test_release_doesnt_send_file_if_not_speciofied_release(self, mock_GitHubGateway):
        self.setup_mock(mock_GitHubGateway)
        self.kwargs['owner'] = 'Big'
        self.kwargs['repo'] = 'Canary'
        self.kwargs['files'] = []

        mock_open_file = mock_open()

        with patch('github_release.open', mock_open_file, create=True):
            ghr = GitHubRelease(**self.kwargs)
            ghr.release()

        self.assertFalse(mock_GitHubGateway.return_value.post_file.called)
    def test_release_calls_post_file_with_correct_url_and_data_for_release(self, mock_GitHubGateway):
        self.setup_mock(mock_GitHubGateway)
        self.kwargs['owner'] = 'Big'
        self.kwargs['repo'] = 'Canary'
        self.kwargs['files'] = self.files
        expected_params= {"name": "test_upload.txt"}

        mock_open_file = mock_open()

        with patch('github_release.open', mock_open_file, create=True):
            mock_file = mock_open_file.return_value
            ghr = GitHubRelease(**self.kwargs)
            ghr.release()

        mock_GitHubGateway.return_value.post_file.assert_called_with(self.expected_upload_url, mock_file, expected_params)
    def test_release_calls_post_json_data_with_correct_url_and_data_for_release(self, mock_GitHubGateway):
        self.setup_mock(mock_GitHubGateway)
        self.kwargs['owner'] = 'Big'
        self.kwargs['repo'] = 'Canary'
        expected_url = self.expected_api + '/repos/Big/Canary/releases'
        expected_data = json.dumps({
              "tag_name": self.kwargs['tag'],
              "target_commitish": "master",
              "name": self.kwargs['name_template'],
              "body": self.kwargs['description_template'],
              "draft": False,
              "prerelease": False
            }, sort_keys=True, indent=4, separators=(',', ': '))

        ghr = GitHubRelease(**self.kwargs)
        ghr.release()

        mock_GitHubGateway.return_value.post_json_data.assert_called_with(expected_url, expected_data)