Skip to content

Commit 2ebc38e

Browse files
Add routing capability (#104)
* Add routing capability
1 parent 0557311 commit 2ebc38e

File tree

12 files changed

+304
-254
lines changed

12 files changed

+304
-254
lines changed

src/functionaltest/java/com/ericsson/ei/frontend/TestSubscriptionHandling.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import org.apache.http.impl.client.CloseableHttpClient;
1010
import org.junit.Test;
11-
import org.openqa.selenium.JavascriptExecutor;
1211
import org.openqa.selenium.support.ui.WebDriverWait;
1312
import org.springframework.boot.test.mock.mockito.MockBean;
1413

@@ -31,8 +30,6 @@ public class TestSubscriptionHandling extends SeleniumBaseClass {
3130
private static final String SUBSCRIPTION_FOR_UPLOAD_FILE_PATH = String.join(File.separator, "src", "functionaltest",
3231
"resources", "responses", "SubscriptionForUploadCase.json");
3332

34-
private JavascriptExecutor js;
35-
3633
@MockBean
3734
protected CloseableHttpClient mockedHttpClient;
3835

@@ -56,6 +53,7 @@ public void testSubscriptionHandlingWithLDAPEnabled() throws Exception {
5653
String response = getJSONStringFromFile(SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH);
5754

5855
subscriptionPage.clickReload(response);
56+
subscriptionPage.refreshPage();
5957

6058
String expandButtonXPath = "//tr[contains(.,'Subscription1')]/td[1]";
6159
String viewButtonXPath = "(//button[@id='view-Subscription1'])";
@@ -66,32 +64,26 @@ public void testSubscriptionHandlingWithLDAPEnabled() throws Exception {
6664
assert (subscriptionPage.buttonExistByXPath(editButtonXPath));
6765
assert (subscriptionPage.buttonExistByXPath(viewButtonXPath));
6866

69-
// Given LDAP is enabled, "Reload" subscriptions and then click
67+
// Given LDAP is enabled, "Reload" subscriptions and reload
7068
// subscription page with LDAP enabled with unauthorized user names
7169
// Verify that subscriptions exists but only with "View" button
7270
String responseSub = getJSONStringFromFile(SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH_LDAP);
7371
String responseAuth = "{\"security\":true}";
7472

7573
subscriptionPage.clickReloadLDAP(responseSub, responseAuth);
76-
indexPageObject.clickSubscriptionPage();
74+
subscriptionPage.refreshPage();
7775

7876
assert (subscriptionPage.clickExpandButtonByXPath(expandButtonXPath));
7977
assert (subscriptionPage.buttonDoesNotExistByXPath(deleteButtonXPath));
8078
assert (subscriptionPage.buttonDoesNotExistByXPath(editButtonXPath));
8179
assert (subscriptionPage.buttonExistByXPath(viewButtonXPath));
8280

83-
// Given LDAP is enabled, "Reload" subscriptions and then click
84-
// subscription page with LDAP enabled with both unauthorized and
85-
// unauthorized user names (in this case authorized user is "ABCD" with
86-
// subscriptions, "subscription1" and "subscription2") Verify that
87-
// current user can see only their own subscriptions' edit and delete
88-
// buttons.
89-
String keyForUser = "currentUser";
90-
String valueForUser = "ABCD";
91-
92-
js = (driver);
93-
js.executeScript(String.format("window.localStorage.setItem('%s','%s');", keyForUser, valueForUser));
94-
indexPageObject.clickSubscriptionPage();
81+
// Given LDAP is enabled, reload the index page and mock the user response as
82+
// user 'ABCD'. Verify that current user can see only their own subscriptions'
83+
// edit and delete buttons.
84+
String responseUser = "{\"user\":\"ABCD\"}";
85+
86+
indexPageObject.loadPageLDAP(responseAuth, responseUser);
9587

9688
assert (subscriptionPage.textExistsInTable("Subscription1"));
9789
assert (subscriptionPage.clickExpandButtonByXPath(expandButtonXPath));

src/functionaltest/java/com/ericsson/ei/frontend/pageobjects/IndexPage.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.ericsson.ei.frontend.pageobjects;
22

3+
import org.apache.http.client.ClientProtocolException;
34
import org.apache.http.client.methods.CloseableHttpResponse;
5+
import org.apache.http.client.methods.HttpRequestBase;
46
import org.apache.http.impl.client.CloseableHttpClient;
57

68
import org.mockito.Mockito;
@@ -33,6 +35,17 @@ public IndexPage loadPage() {
3335
return this;
3436
}
3537

38+
public void loadPageLDAP(String responseAuth, String responseUser) throws ClientProtocolException, IOException {
39+
CloseableHttpResponse responseDataAuth = this.createMockedHTTPResponse(responseAuth, 200);
40+
CloseableHttpResponse responseDataUser = this.createMockedHTTPResponse(responseUser, 200);
41+
Mockito.doReturn(responseDataAuth).when(mockedHttpClient).execute(
42+
Mockito.argThat(request -> ((HttpRequestBase) request).getURI().toString().endsWith("auth")));
43+
Mockito.doReturn(responseDataUser).when(mockedHttpClient)
44+
.execute(Mockito.argThat(request -> ((HttpRequestBase) request).getURI().toString().endsWith("auth/login")));
45+
driver.get(baseUrl);
46+
waitForJQueryToLoad();
47+
}
48+
3649
public String getTitle() {
3750
return driver.getTitle();
3851
}

src/functionaltest/java/com/ericsson/ei/frontend/pageobjects/PageBaseClass.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public PageBaseClass(CloseableHttpClient mockedHttpClient, FirefoxDriver driver,
3333
PageFactory.initElements(driver, this);
3434
}
3535

36+
public void refreshPage() {
37+
driver.navigate().refresh();
38+
}
39+
3640
public void waitForJQueryToLoad() {
3741
try {
3842
WebDriverWait webDriverWait = new WebDriverWait(driver, 10);

src/functionaltest/java/com/ericsson/ei/frontend/pageobjects/SubscriptionPage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ public void clickReloadLDAP(String response, String responseAuth) throws IOExcep
267267
CloseableHttpResponse responseData = this.createMockedHTTPResponse(response, 200);
268268
CloseableHttpResponse responseDataAuth = this.createMockedHTTPResponse(responseAuth, 200);
269269
Mockito.doReturn(responseData).when(mockedHttpClient).execute(
270-
Mockito.argThat(request -> ((HttpRequestBase) request).getURI().toString().contains("subscriptions")));
270+
Mockito.argThat(request -> ((HttpRequestBase) request).getURI().toString().endsWith("subscriptions")));
271271
Mockito.doReturn(responseDataAuth).when(mockedHttpClient)
272-
.execute(Mockito.argThat(request -> ((HttpRequestBase) request).getURI().toString().contains("auth")));
272+
.execute(Mockito.argThat(request -> ((HttpRequestBase) request).getURI().toString().endsWith("auth")));
273273
WebElement reloadBtn = new WebDriverWait(driver, TIMEOUT_TIMER)
274274
.until(ExpectedConditions.elementToBeClickable(By.id("reloadButton")));
275275
reloadBtn.click();

src/main/java/com/ericsson/ei/frontend/WebController.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ public String login(Model model) {
6666
return "login";
6767
}
6868

69-
// Added documentation for JMESPath rules usage
70-
@RequestMapping("/jmesPathRulesSetUp.html")
71-
public String jmesPathRulesSetUp(Model model) {
72-
return "jmesPathRulesSetUp";
73-
}
74-
7569
@RequestMapping("/add-instances.html")
7670
public String addInstance(Model model) {
7771
model.addAttribute("frontendServiceUrl", frontEndUtils.getFrontEndServiceUrl());

src/main/resources/static/assets/navigo/7.1.2/navigo.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/resources/static/js/add-instances.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ jQuery(document).ready(function() {
22
var frontendServiceUrl = $('#frontendServiceUrl').text();
33
var frontendServicePath = "/backend";
44
function instanceModel() {
5+
var router = new Navigo(null, true, '#');
6+
57
var self = this;
68
self.instance = {
79
name: ko.observable(""),
@@ -30,7 +32,7 @@ function instanceModel() {
3032
},
3133
success: function (responseData, XMLHttpRequest, textStatus) {
3234
$.jGrowl(responseData.message, {sticky: false, theme: 'Notify'});
33-
$("#mainFrame").load("switch-backend.html");
35+
router.navigate('switch-backend');
3436
}
3537
});
3638
}

src/main/resources/static/js/login.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11

22
jQuery(document).ready(function() {
3-
var frontendServiceUrl = $('#frontendServiceUrl').text();
3+
var router = new Navigo(null, true, '#');
4+
var frontendServiceUrl = $('#frontendServiceUrl').text();
5+
6+
function checkBackendSecured() {
7+
$.ajax({
8+
url: frontendServiceUrl + "/auth",
9+
type: "GET",
10+
contentType: "application/string; charset=utf-8",
11+
error: function (data) {
12+
router.navigate('subscriptions');
13+
},
14+
success: function (data) {
15+
var currentUser = localStorage.getItem("currentUser");
16+
var isSecured = JSON.parse(ko.toJSON(data)).security;
17+
if (isSecured == false || (isSecured == true && currentUser != null)) {
18+
router.navigate('subscriptions');
19+
}
20+
}
21+
});
22+
}
23+
24+
checkBackendSecured();
25+
426
// /Start ## Knockout ####################################################
527
function loginModel() {
628
this.userState = {
@@ -36,19 +58,18 @@ var frontendServiceUrl = $('#frontendServiceUrl').text();
3658
var currentUser = JSON.parse(ko.toJSON(responseData)).user;
3759
$.jGrowl("Welcome " + currentUser, { sticky : false, theme : 'Notify' });
3860
doIfUserLoggedIn(currentUser);
39-
$("#mainFrame").load("subscriptionpage.html");
40-
},
41-
complete : function (request, textStatus) { }
61+
router.navigate('subscriptions');
62+
}
4263
});
4364
}
4465

45-
function doIfUserLoggedIn(name) {
46-
localStorage.removeItem("currentUser");
47-
localStorage.setItem("currentUser", name);
48-
$("#ldapUserName").text(name);
49-
$("#loginBlock").hide();
50-
$("#logoutBlock").show();
51-
}
66+
function doIfUserLoggedIn(name) {
67+
localStorage.removeItem("currentUser");
68+
localStorage.setItem("currentUser", name);
69+
$("#ldapUserName").text(name);
70+
$("#loginBlock").hide();
71+
$("#logoutBlock").show();
72+
}
5273

5374
var observableObject = $("#viewModelDOMObject")[0];
5475
ko.cleanNode(observableObject);

0 commit comments

Comments
 (0)