ashb commented on a change in pull request #15042:
URL: https://github.com/apache/airflow/pull/15042#discussion_r603960819



##########
File path: tests/api_connexion/endpoints/test_auth_endpoint.py
##########
@@ -0,0 +1,273 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from unittest import mock
+
+import pytest
+from flask_appbuilder.const import AUTH_DB, AUTH_LDAP, AUTH_OAUTH, 
AUTH_REMOTE_USER
+
+from tests.test_utils.api_connexion_utils import delete_user
+from tests.test_utils.fab_utils import create_user
+
+
[email protected](scope="module")
+def configured_app(minimal_app_for_api):
+    app = minimal_app_for_api
+    create_user(app, username="test", role_name="Test")  # type: ignore
+
+    yield app
+
+    delete_user(app, username="test")  # type: ignore
+
+
+class TestLoginEndpoint:
+    @pytest.fixture(autouse=True)
+    def setup_attrs(self, configured_app) -> None:
+        self.app = configured_app
+        self.client = self.app.test_client()  # type:ignore
+
+    def auth_type(self, auth):
+        self.app.config['AUTH_TYPE'] = auth
+
+
+def auth_type(self, auth):
+    self.app.config['AUTH_TYPE'] = auth
+
+
+class TestDBLoginEndpoint(TestLoginEndpoint):
+    def test_user_can_login(self):
+        self.auth_type(AUTH_DB)
+        payload = {"username": "test", "password": "test"}
+        response = self.client.post('api/v1/auth-dblogin', json=payload)
+        assert response.json['username'] == 'test'
+        cookie = next(
+            (cookie for cookie in self.client.cookie_jar if cookie.name == 
"access_token_cookie"), None
+        )
+        assert cookie is not None
+        assert isinstance(cookie.value, str)
+
+    def test_logged_in_user_cant_relogin(self):
+        self.auth_type(AUTH_DB)
+        payload = {"username": "test", "password": "test"}
+        response = self.client.post('api/v1/auth-dblogin', json=payload)
+        assert response.json['username'] == 'test'
+        response = self.client.post('api/v1/auth-dblogin', json=payload)
+        assert response.status_code == 401
+        assert response.json['detail'] == "Client already authenticated"
+
+    def test_incorrect_username_raises(self):
+        self.auth_type(AUTH_DB)
+        payload = {"username": "tests", "password": "test"}
+        response = self.client.post('api/v1/auth-dblogin', json=payload)
+        assert response.status_code == 404

Review comment:
       This should be a 401 Forbidden, not 404 Not Found.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to