From c0c470fbf4fdad80039af5719e26807ca1397ccd Mon Sep 17 00:00:00 2001
From: Mark <mark@openappstack.net>
Date: Fri, 27 Mar 2020 16:21:07 +0100
Subject: [PATCH] Add tests for remember me button

---
 login_provider/app.py                         |  2 +-
 login_provider/templates/skip.html            |  4 +--
 .../test/behave/features/login.feature        | 33 +++++++++++++++++++
 .../test/behave/features/steps/login.py       | 11 +++++--
 4 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/login_provider/app.py b/login_provider/app.py
index bf8052e..67ccce1 100644
--- a/login_provider/app.py
+++ b/login_provider/app.py
@@ -89,7 +89,7 @@ def login():
             HYDRA.invalidate_login_sessions(login_request.subject);
             return redirect(login_request.reject(
                 "Login cancelled",
-                error_description="Login was cancelled and user session was terminated "))
+                error_description="Login was cancelled and user session was terminated"))
         else:
             return render_template('skip.html', challenge=challenge, logo=login_request.client.logo_uri, application_name=login_request.client.client_name, username=login_request.subject)
 
diff --git a/login_provider/templates/skip.html b/login_provider/templates/skip.html
index 3a867bd..64b2474 100644
--- a/login_provider/templates/skip.html
+++ b/login_provider/templates/skip.html
@@ -8,7 +8,7 @@
   {% endif %}
   <h1>Log in to {{ application_name }}</h1>
   <div style="width: 100%; margin-bottom: 5px; overflow: auto">
-      <div style="width:60%; float:left"><button onclick="window.location.href = '/login?login_challenge={{ challenge }}&skip=true';">Continue with {{ username }}</button></div>
-      <div style="width:40%; float:left;"><button onclick="window.location.href = '/login?login_challenge={{ challenge }}&logout=true';">Logout</button></div>
+      <div style="width:60%; float:left"><button id="continue" onclick="window.location.href = '/login?login_challenge={{ challenge }}&skip=true';">Continue with {{ username }}</button></div>
+      <div style="width:40%; float:left;"><button id="logout" onclick="window.location.href = '/login?login_challenge={{ challenge }}&logout=true';">Logout</button></div>
   </div>
 </div>
diff --git a/test/login_logout/test/behave/features/login.feature b/test/login_logout/test/behave/features/login.feature
index 84c8d00..3279b36 100644
--- a/test/login_logout/test/behave/features/login.feature
+++ b/test/login_logout/test/behave/features/login.feature
@@ -52,3 +52,36 @@ Scenario: Login with an invalid user without
     And I expect that element "body" contains the text "error"
     And I expect that element "body" contains the text "Login denied"
     And I expect that element "body" contains the text "Invalid username or password"
+
+Scenario: Login with a valid user and remember session
+    Given the oauth client "home" URL was opened
+    And the element "input#username" is visible
+    And the element "input#remember" is visible
+    When I enter the "username" in the inputfield "input#username"
+    And I enter the "password" in the inputfield "input#password"
+    And I click on the element "input#remember"
+    And I click on the button "input#submit"
+    Then I wait on element "input#password" for 1000ms to not exist
+    And I expect that element "input#username" does not exist
+    And I expect that the path is "/callback"
+    And I expect that element "body" contains the text "access_token"
+
+Scenario: Login without providing credentials
+    Given the oauth client "logout" URL was opened
+    And I pause for 1000ms
+    And there is no element "input#username" on the page
+    And there is no element "input#password" on the page
+    And the element "button#continue" is visible
+    When I click on the element "button#continue"
+    Then I wait on element "input#password" for 1000ms to not exist
+    And I expect that element "input#username" does not exist
+    And I expect that the path is "/callback"
+    And I expect that element "body" contains the text "access_token"
+
+Scenario: Login with different user
+    Given the oauth client "logout" URL was opened
+    And I pause for 1000ms
+    And the element "button#continue" is visible
+    When I click on the element "button#logout"
+    Then I expect that the "error" in the json output is "Login cancelled"
+    And I expect that the "error_description" in the json output is "Login was cancelled and user session was terminated"
diff --git a/test/login_logout/test/behave/features/steps/login.py b/test/login_logout/test/behave/features/steps/login.py
index 4be7355..16da434 100644
--- a/test/login_logout/test/behave/features/steps/login.py
+++ b/test/login_logout/test/behave/features/steps/login.py
@@ -32,11 +32,18 @@ def step_impl(context, element, variable):
 
 @then(u'I expect that the "{variable}" in the json output is "{value}"')
 def step_impl(context, variable, value):
-    assert context.oauth[value] == get_value_from_json_body(context, variable)
+    if value in context.oauth:
+        assert context.oauth[value] == get_value_from_json_body(context, variable)
+    else:
+        assert value == get_value_from_json_body(context, variable)
+
 
 @then(u'I expect that the "{variable}" in the json output contains "{value}"')
 def step_impl(context, variable, value):
-    assert context.oauth[value] in get_value_from_json_body(context, variable)
+    if value in context.oauth:
+        assert context.oauth[value] in get_value_from_json_body(context, variable)
+    else:
+        assert value in get_value_from_json_body(context, variable)
 
 def get_value_from_json_body(context, key):
     obj_serialized = context.behave_driver.get_element("body").text
-- 
GitLab