def test_authenticated_user(self):
        # require super user permissions
        user = mixer.blend("auth.User", is_superuser=False)
        url = reverse(self.URL_NAME)
        request = RequestFactory().get(url)
        request.user = user

        with pytest.raises(PermissionDenied):
            views.cisco_eox_query(request)
    def test_anonymous_default(self):
        url = reverse(self.URL_NAME)
        request = RequestFactory().get(url)
        request.user = AnonymousUser()
        response = views.cisco_eox_query(request)

        assert response.status_code == 302
    def test_superuser_access(self):
        # require super user permissions
        user = mixer.blend("auth.User", is_superuser=True)
        url = reverse(self.URL_NAME)
        request = RequestFactory().get(url)
        request.user = user
        response = views.cisco_eox_query(request)

        assert response.status_code == 200
    def test_post_with_api_errors(self, monkeypatch):
        def mock_api_call_failed():
            raise CiscoApiCallFailed()

        def mock_connection_failed_exception():
            raise ConnectionFailedException()

        monkeypatch.setattr(AppSettings, "is_cisco_api_enabled", lambda self: True)
        monkeypatch.setattr(api_crawler, "update_cisco_eox_database",
                            lambda api_query: mock_api_call_failed())

        user = mixer.blend("auth.User", is_superuser=True)

        url = reverse(self.URL_NAME)
        data = {
            "sync_cisco_eox_states_now": "on",
            "sync_cisco_eox_states_query": "WS-C2960-24T-S"
        }
        request = RequestFactory().post(url, data=data)
        request.user = user
        response = views.cisco_eox_query(request)

        assert response.status_code == 200
        assert "Cisco API call failed" in response.content.decode()

        monkeypatch.setattr(api_crawler, "update_cisco_eox_database",
                            lambda api_query: mock_connection_failed_exception())

        url = reverse(self.URL_NAME)
        data = {
            "sync_cisco_eox_states_now": "on",
            "sync_cisco_eox_states_query": "WS-C2960-24T-S"
        }
        request = RequestFactory().post(url, data=data)
        request.user = user
        response = views.cisco_eox_query(request)

        assert response.status_code == 200
        assert "Cannot contact Cisco API" in response.content.decode()
    def test_post(self, monkeypatch):
        result = [
            {
                "PID": None,
                "blacklist": False,
                "updated": False,
                "created": False,
                "message": "No product update required"
            }
        ]
        monkeypatch.setattr(AppSettings, "is_cisco_api_enabled", lambda self: True)
        monkeypatch.setattr(api_crawler, "update_cisco_eox_database", lambda api_query: result)

        user = mixer.blend("auth.User", is_superuser=True)

        # test post values without explicit checkbox
        url = reverse(self.URL_NAME)
        data = {
            "sync_cisco_eox_states_query": "WS-C2960-24T-S"
        }
        request = RequestFactory().post(url, data=data)
        request.user = user
        response = views.cisco_eox_query(request)

        assert response.status_code == 200
        assert "No product update required" not in response.content.decode()
        assert "please select the "execute it now" in response.content.decode()

        url = reverse(self.URL_NAME)
        data = {
            "sync_cisco_eox_states_now": "on"
        }
        request = RequestFactory().post(url, data=data)
        request.user = user
        response = views.cisco_eox_query(request)

        assert response.status_code == 200
        assert "Query not specified." in response.content.decode()

        url = reverse(self.URL_NAME)
        data = {
            "sync_cisco_eox_states_now": "on",
            "sync_cisco_eox_states_query": ""
        }
        request = RequestFactory().post(url, data=data)
        request.user = user
        response = views.cisco_eox_query(request)

        assert response.status_code == 200
        assert "Please specify a valid query" in response.content.decode()

        url = reverse(self.URL_NAME)
        data = {
            "sync_cisco_eox_states_now": "on",
            "sync_cisco_eox_states_query": "WS-C29 60-24T-S"
        }
        request = RequestFactory().post(url, data=data)
        request.user = user
        response = views.cisco_eox_query(request)

        assert response.status_code == 200
        assert "Invalid query 'WS-C29 60-24T-S': not executed" in response.content.decode()

        url = reverse(self.URL_NAME)
        data = {
            "sync_cisco_eox_states_now": "on",
            "sync_cisco_eox_states_query": "WS-C2960-24T-S"
        }
        request = RequestFactory().post(url, data=data)
        request.user = user
        response = views.cisco_eox_query(request)

        assert response.status_code == 200
        assert "No product update required" in response.content.decode()