from qtpy.QtCore import Qt
from unittest import mock


class TestBaseLoginPage:
    def test_dialog_clicked_back_buttons(self):
        self.qtbot.wait_for_window_shown(self.widget)

        with self.qtbot.wait_signal(self.widget.button_back.clicked,
                                    raising=True), mock.patch.object(self.widget, 'reject'):
            self.qtbot.mouseClick(self.widget.button_back, Qt.LeftButton)
            self.widget.reject.assert_called()
            self.widget.main_dialog.show.assert_called()

    def test_login_button_enabled(self):
        self.qtbot.wait_for_window_shown(self.widget)

        self.widget.text_username.setText("TEST_USER")
        self.widget.text_password.setText("TEST_PASSWORD")
        self.widget.text_username.textEdited.emit(None)
        assert self.widget.button_login.isEnabled() is True

        self.widget.text_username.setText("")
        self.widget.text_password.setText("")
        self.widget.text_username.textEdited.emit(None)
        assert self.widget.button_login.isEnabled() is False

    def test_login(self):
        self.qtbot.wait_for_window_shown(self.widget)

        self.widget.text_username.setText("TEST_USER")
        self.widget.text_password.setText("TEST_PASSWORD")
        self.widget.text_username.textEdited.emit(None)

        with mock.patch.object(self.widget.config, 'get'), \
                self.qtbot.wait_signal(self.widget.button_login.clicked, raising=True):

            self.widget.config.get.return_value = "https://api.domain.org"
            self.widget.anaconda_api.login.return_value = mock.Mock()
            self.qtbot.wait(1500)
            self.qtbot.mouseClick(self.widget.button_login, Qt.LeftButton)
            self.widget.anaconda_api.client_reload.assert_called()

    def test_login_finished(self):
        worker, output, error = mock.Mock(), "TOKEN_MOCK", ""
        self.widget._finished(worker, output, error)
        with mock.patch.object(self.widget, 'accept'):
            self.widget.accept.assset_called()
            self.widget.main_dialog.accept.assert_called()

    def test_login_finished_with_error(self):
        worker, output, error = mock.Mock(), "", "error"
        with self.qtbot.wait_signal(self.widget.sig_authentication_failed, raising=True):
            self.widget._finished(worker, output, error)
        with mock.patch.object(self.widget.tracker, 'track_event'), mock.patch.object(self.widget, 'accept'):
            self.widget.accept.assset_not_called()
            self.widget.main_dialog.accept.assset_not_called()
            self.widget.tracker.track_event.accept.assset_called()


class TestBaseSetDomainPage:
    def test_set_domain(self):
        domain = "https://api.someedition.com"
        self.widget.input_line.setText(domain)

        with mock.patch.object(self.widget, 'check_text'), mock.patch.object(self.widget, 'accept'), \
                mock.patch.object(self.widget, 'anaconda_api'), mock.patch.object(self.widget.config, 'set'), \
                self.qtbot.wait_signal(self.widget.button_apply.clicked, raising=True):

            self.widget.check_text.return_value = True
            self.qtbot.mouseClick(self.widget.button_apply, Qt.LeftButton)
            self.widget.accept.assert_called()
            self.widget.check_text.assert_called()
            self.widget.anaconda_api.client_reload.assert_called()
            self.widget.config.set.assert_called()

    def test_set_domain_failed(self):
        with mock.patch.object(self.widget, 'check_text'), mock.patch.object(self.widget, 'accept'), \
                mock.patch.object(self.widget, 'anaconda_api'), mock.patch.object(self.widget.config, 'set'), \
                self.qtbot.wait_signal(self.widget.button_apply.clicked, raising=True):
            self.widget.check_text.return_value = False
            self.qtbot.mouseClick(self.widget.button_apply, Qt.LeftButton)
            self.widget.check_text.assert_called()
            self.widget.accept.assert_not_called()
            self.widget.anaconda_api.client_reload.assert_not_called()
            self.widget.config.set.assert_not_called()
