import unittest
from unittest.mock import MagicMock
from nose.tools import eq_, assert_raises
from helix.Services.doc_gen_service import DocGenService, DocGenServiceError


class DocGenServiceTest(unittest.TestCase):
    def setUp(self):
        self.request_maker = MagicMock()
        self.request_builder = MagicMock()
        self.request_builder.build.return_value = {"key": "value"}
        self.user_values = MagicMock()
        self.user_values.system_type.return_value = "MySystemType"

        self.subject = DocGenService(self.request_maker, self.request_builder)

    def test_generate_everything_valid(self):
        expected = "expected value"
        post_return = MagicMock()
        post_return.content = expected
        post_return.status_code = 200
        self.request_maker.post.return_value = post_return

        eq_(self.subject.generate(), expected)

        self.request_builder.build.assert_called_once_with()
        self.request_maker.post.assert_called_once_with('https://dcs.us.sunpower.com/ws/docgen/docx/generatePdf', "{\"key\": \"value\"}", headers={'content-type': 'application/json'})

    def test_generate_returns_400(self):
        expected = "some error"
        post_return = MagicMock()
        post_return.content = expected
        post_return.status_code = 400
        self.request_maker.post.return_value = post_return

        assert_raises(DocGenServiceError, self.subject.generate)
        self.request_builder.build.assert_called_once_with()
        self.request_maker.post.assert_called_once_with('https://dcs.us.sunpower.com/ws/docgen/docx/generatePdf', "{\"key\": \"value\"}", headers={'content-type': 'application/json'})

    def test_generate_returns_500(self):
        expected = "some error"
        post_return = MagicMock()
        post_return.content = expected
        post_return.status_code = 500
        self.request_maker.post.return_value = post_return

        assert_raises(DocGenServiceError, self.subject.generate)
        self.request_builder.build.assert_called_once_with()
        self.request_maker.post.assert_called_once_with('https://dcs.us.sunpower.com/ws/docgen/docx/generatePdf', "{\"key\": \"value\"}", headers={'content-type': 'application/json'})
