Skip to content

Commit 87609f7

Browse files
author
Anders Breid
authored
Fix multiple users may use frontend, request outside web gui. (#65)
* Fix multiple users may use frontend, request outside web gui. - Funktion to sent backend url as parameter added. - Gui users may now switch backend without affecting each other. - Before saving new instances the code will check if any new instances has been added to not overwrite instances created by other users. - switch-backend delete now recieves the backend to delete and not the backends to not delete. - If file is invalid or non existand it will be recreated. - Default back end properties may be specified in application-properties. - Default parameters may not be removed from gui but must be removed from application-properties first. - Unit tests have been added and updated. * Modified functional tests to work with the new utils changes. * Fixed some functional tests. * fixed code comments * Updated after comments * Fixed minor issues with switching back end * Added unittest for curl commando possibilities. * Extended time out to 11s * fixed some duplicated functions from comments. * updated after comments * updated after comments * Fixed functional tests * Missed to add 2 changes * updated after comments
1 parent ce1d691 commit 87609f7

39 files changed

+1920
-762
lines changed

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

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
package com.ericsson.ei.frontend;
22

3-
import com.ericsson.ei.config.SeleniumConfig;
4-
import org.apache.tomcat.util.http.fileupload.FileUtils;
3+
import static org.junit.Assert.fail;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
510

11+
import org.apache.tomcat.util.http.fileupload.FileUtils;
612
import org.junit.After;
713
import org.junit.Before;
814
import org.junit.Ignore;
915
import org.junit.runner.RunWith;
1016
import org.mockito.InjectMocks;
11-
1217
import org.mockito.MockitoAnnotations;
1318
import org.openqa.selenium.firefox.FirefoxDriver;
14-
1519
import org.springframework.beans.factory.annotation.Autowired;
1620
import org.springframework.boot.test.context.SpringBootTest;
1721
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
1822
import org.springframework.boot.web.server.LocalServerPort;
1923
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2024

21-
import java.io.File;
22-
import java.io.FileWriter;
23-
import java.io.IOException;
24-
import java.nio.charset.StandardCharsets;
25-
import java.nio.file.Files;
26-
import java.nio.file.Paths;
27-
28-
import static org.junit.Assert.fail;
25+
import com.ericsson.ei.config.SeleniumConfig;
26+
import com.ericsson.ei.frontend.utils.BackEndInstanceFileUtils;
27+
import com.ericsson.ei.frontend.utils.BackEndInstancesUtils;
28+
import com.ericsson.ei.frontend.utils.WebControllerUtils;
2929

3030
@Ignore
3131
@RunWith(SpringJUnit4ClassRunner.class)
@@ -39,43 +39,51 @@ public class SeleniumBaseClass {
3939
EIRequestsController eIRequestsController;
4040

4141
@Autowired
42-
WebController webController;
42+
WebControllerUtils webControllerUtils;
4343

4444

4545
protected FirefoxDriver driver;
4646
protected String baseUrl;
4747

4848
private StringBuffer verificationErrors = new StringBuffer();
4949

50-
private static final String BACKEND_INSTANCES_INFORMATION_FILEPATH = String.join(
51-
File.separator, "src", "main", "resources", "EIBackendInstancesInformation.json");
52-
private String default_instances_information;
50+
private String filePath = "";
5351

52+
@Autowired
53+
private BackEndInstanceFileUtils backEndInstanceFileUtils;
54+
55+
@Autowired
56+
protected BackEndInstancesUtils backEndInstancesUtils;
57+
5458
@Before
5559
public void setUp() throws Exception {
56-
default_instances_information = getJSONStringFromFile(BACKEND_INSTANCES_INFORMATION_FILEPATH);
60+
File tempFile = File.createTempFile("tempfile", ".json");
61+
tempFile.deleteOnExit();
62+
63+
filePath = tempFile.getAbsolutePath().toString();
64+
Files.write(Paths.get(filePath), "[]".getBytes());
65+
backEndInstanceFileUtils.setEiInstancesPath(filePath);
66+
67+
backEndInstancesUtils.setDefaultBackEndInstanceToNull();
68+
backEndInstancesUtils.setDefaultBackEndInstance("test", "localhost", 12345, "", true);
5769
MockitoAnnotations.initMocks(this);
58-
webController.setFrontendServicePort(randomServerPort);
70+
webControllerUtils.setFrontendServicePort(randomServerPort);
5971

6072
driver = SeleniumConfig.initFirefoxDriver();
6173
baseUrl = SeleniumConfig.getBaseUrl(randomServerPort);
6274
}
6375

6476
@After
6577
public void tearDown() throws Exception {
66-
6778
File tempDownloadDirectory = SeleniumConfig.getTempDownloadDirectory();
6879
FileUtils.deleteDirectory(tempDownloadDirectory);
6980

70-
// Reset whats inside EIBackendInstancesInformation since test will fail if run multiple times otherwise
71-
FileWriter backendInstancesInformationWriter = new FileWriter(BACKEND_INSTANCES_INFORMATION_FILEPATH, false);
72-
backendInstancesInformationWriter.write(default_instances_information);
73-
backendInstancesInformationWriter.close();
74-
7581
String verificationErrorString = verificationErrors.toString();
7682
if (!verificationErrorString.equals("")) {
7783
fail(verificationErrorString);
7884
}
85+
86+
backEndInstancesUtils.setDefaultBackEndInstanceToNull();
7987
}
8088

8189
protected static String getJSONStringFromFile(String filepath) throws IOException {
Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.ericsson.ei.frontend;
22

3-
import org.apache.http.impl.client.CloseableHttpClient;
4-
import org.junit.*;
5-
import org.openqa.selenium.JavascriptExecutor;
6-
import org.openqa.selenium.support.ui.WebDriverWait;
7-
import org.springframework.boot.test.mock.mockito.MockBean;
8-
9-
import static org.junit.Assert.*;
3+
import static org.junit.Assert.assertEquals;
104

115
import java.io.File;
126
import java.nio.file.Files;
137
import java.nio.file.Paths;
148

9+
import org.apache.http.impl.client.CloseableHttpClient;
10+
import org.junit.Test;
11+
import org.openqa.selenium.JavascriptExecutor;
12+
import org.openqa.selenium.support.ui.WebDriverWait;
13+
import org.springframework.boot.test.mock.mockito.MockBean;
14+
1515
import com.ericsson.ei.config.SeleniumConfig;
1616
import com.ericsson.ei.frontend.pageobjects.IndexPage;
1717
import com.ericsson.ei.frontend.pageobjects.SubscriptionPage;
@@ -38,7 +38,7 @@ public class SubscriptionHandlingFunctionality extends SeleniumBaseClass {
3838

3939
@Test
4040
public void testSubscription() throws Exception {
41-
// Open index page
41+
// Open index page.
4242
IndexPage indexPageObject = new IndexPage(mockedHttpClient, driver, baseUrl);
4343
indexPageObject.loadPage();
4444

@@ -48,57 +48,65 @@ public void testSubscription() throws Exception {
4848
assert (new WebDriverWait(driver, 10)
4949
.until((webdriver) -> subscriptionPage.presenceOfHeader(subscriptionHeaderID)));
5050

51-
// Press "Reload" button without enabling LDAP and verify that two subscriptions
52-
// with names "Subscription1" and "Subscription2" are present AND there exists "edit" and
53-
// "delete buttons" for unauthorized user "ABCD"
54-
String response = this.getJSONStringFromFile(SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH);
51+
// Press "Reload" button without enabling LDAP and verify that two
52+
// subscriptions with names "Subscription1" and "Subscription2" are
53+
// present AND there exists "edit" and" delete buttons" for unauthorized
54+
// user "ABCD"
55+
String response = getJSONStringFromFile(SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH);
5556
String viewButtonXPath = "(//button[@id='view-Subscription1'])[2]";
5657
String editButtonXPath = "(//button[@id='edit-Subscription1'])[2]";
5758
String deleteButtonXPath = "(//button[@id='delete-Subscription1'])[2]";
5859
String expandButtonXPath = "//tr[contains(.,'Subscription1')]/td[1]";
60+
5961
subscriptionPage.clickReload(response);
62+
6063
assert (subscriptionPage.textExistsInTable("Subscription1"));
6164
assert (subscriptionPage.textExistsInTable("Subscription2"));
6265
assert (subscriptionPage.clickElementByXPath(expandButtonXPath));
6366
assert (subscriptionPage.buttonExist(deleteButtonXPath));
6467
assert (subscriptionPage.buttonExist(editButtonXPath));
6568
assert (subscriptionPage.buttonExist(viewButtonXPath));
6669

67-
// Given LDAP is enabled, "Reload" subscriptions and then click subscription
68-
// page with LDAP enabled with unauthorized user names
70+
// Given LDAP is enabled, "Reload" subscriptions and then click
71+
// subscription page with LDAP enabled with unauthorized user names
6972
// Verify that subscriptions exists but only with "View" button
70-
String responseSub = this.getJSONStringFromFile(SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH_LDAP);
73+
String responseSub = getJSONStringFromFile(SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH_LDAP);
7174
String responseAuth = "{\"security\":true}";
75+
7276
subscriptionPage.clickReloadLDAP(responseSub, responseAuth);
7377
indexPageObject.clickSubscriptionPage();
78+
7479
assert (subscriptionPage.clickElementByXPath(expandButtonXPath));
7580
assert (!subscriptionPage.buttonExist(deleteButtonXPath));
7681
assert (!subscriptionPage.buttonExist(editButtonXPath));
7782
assert (subscriptionPage.buttonExist(viewButtonXPath));
7883

79-
// Given LDAP is enabled, "Reload" subscriptions and then click subscription
80-
// page with LDAP enabled with both unauthorized and unauthorized user names (in
81-
// this case authorized user is "ABCD" with subscriptions, "subscription1" and
82-
// "subscription2")
83-
// Verify that current user can see only their own subscriptions' edit and
84-
// delete buttons.
84+
// Given LDAP is enabled, "Reload" subscriptions and then click
85+
// subscription page with LDAP enabled with both unauthorized and
86+
// unauthorized user names (in this case authorized user is "ABCD" with
87+
// subscriptions, "subscription1" and "subscription2") Verify that
88+
// current user can see only their own subscriptions' edit and delete
89+
// buttons.
8590
String keyForUser = "currentUser";
8691
String valueForUser = "ABCD";
87-
js = ((JavascriptExecutor) driver);
92+
93+
js = (driver);
8894
js.executeScript(String.format("window.localStorage.setItem('%s','%s');", keyForUser, valueForUser));
8995
indexPageObject.clickSubscriptionPage();
96+
9097
assert (subscriptionPage.textExistsInTable("Subscription1"));
9198
assert (subscriptionPage.clickElementByXPath(expandButtonXPath));
9299
assert (subscriptionPage.buttonExist(deleteButtonXPath));
93100
assert (subscriptionPage.buttonExist(editButtonXPath));
94101
assert (subscriptionPage.buttonExist(viewButtonXPath));
95102

96-
// Now, path for "subscriptions2" with user name "DEF", so user "ABCD" is
97-
// unauthorized for this subscription
103+
// Now, path for "subscriptions2" with user name "DEF", so user "ABCD"
104+
// is unauthorized for this subscription
98105
String viewButtonXPath2 = "(//button[@id='view-Subscription2'])[2]";
99106
String editButtonXPath2 = "(//button[@id='edit-Subscription2'])[2]";
100107
String deleteButtonXPath2 = "(//button[@id='delete-Subscription2'])[2]";
101108
String expandButtonXPath2 = "//tr[contains(.,'Subscription2')]/td[1]";
109+
102110
assert (subscriptionPage.clickElementByXPath(expandButtonXPath2));
103111
assert (subscriptionPage.buttonExist(viewButtonXPath2));
104112
assert (!subscriptionPage.buttonExist(editButtonXPath2));
@@ -114,41 +122,46 @@ public void testSubscription() throws Exception {
114122
indexPageObject.loadPage();
115123
indexPageObject.clickSubscriptionPage().clickReload(response);
116124

117-
// Delete all subscriptions with "Bulk Delete" button and verify that all
118-
// subscriptions are deleted
125+
// Delete all subscriptions with "Bulk Delete" button and verify that
126+
// all subscriptions are deleted
119127
String mockedDeleteResponse = "";
120128
subscriptionPage.clickBulkDelete(mockedDeleteResponse);
121129
assert (!subscriptionPage.textExistsInTable("Subscription1"));
122130
assert (!subscriptionPage.textExistsInTable("Subscription2"));
123131

124132
// Verify that "get template" button works
125-
String mockedTemplateResponse = this.getJSONStringFromFile(SUBSCRIPTION_TEMPLATE_FILE_PATH);
133+
String mockedTemplateResponse = getJSONStringFromFile(SUBSCRIPTION_TEMPLATE_FILE_PATH);
126134
new WebDriverWait(driver, 10).until((webdriver) -> subscriptionPage.presenceOfClickGetTemplateButton());
127135
subscriptionPage.clickDownloadGetTemplate(mockedTemplateResponse);
128136
new WebDriverWait(driver, 10).until((webdriver) -> Files.exists(Paths.get(DOWNLOADED_TEMPLATE_FILE_PATH)));
129-
String getSubscriptionsTemplate = this.getJSONStringFromFile(DOWNLOADED_TEMPLATE_FILE_PATH);
137+
String getSubscriptionsTemplate = getJSONStringFromFile(DOWNLOADED_TEMPLATE_FILE_PATH);
130138
assertEquals(mockedTemplateResponse, getSubscriptionsTemplate);
131139

132140
// Upload a subscription, name as "Subscription_uploaded" with "Upload
133141
// SUbscriptions" button and verify
134-
String mockedUploadResponse = this.getJSONStringFromFile(SUBSCRIPTION_FOR_UPLOAD_FILE_PATH);
142+
String mockedUploadResponse = getJSONStringFromFile(SUBSCRIPTION_FOR_UPLOAD_FILE_PATH);
135143
subscriptionPage.clickUploadSubscriptionFunctionality(DOWNLOADED_TEMPLATE_FILE_PATH, mockedUploadResponse);
136144
assert (subscriptionPage.textExistsInTable("Subscription_uploaded"));
137145

138-
// Click "Add Subscription" button and verify that "Subscription Form" is open
146+
// Click "Add Subscription" button and verify that "Subscription Form"
147+
// is open
139148
subscriptionPage.clickAddSubscription();
140149
String formHeaderID = "formHeader";
141150
assert ((new WebDriverWait(driver, 10).until((webdriver) -> subscriptionPage.presenceOfHeader(formHeaderID))));
142-
143-
// Test form "Cancel" button:Click "Cancel" button and verify that "Subscription Form" is closed
151+
152+
// On subscription form, select the template as "Mail Trigger" and
153+
// verify Test form "Cancel" button:Click "Cancel" button and verify
154+
// that "Subscription Form" is closed
144155
subscriptionPage.clickFormsCancelBtn();
145-
assert (!subscriptionPage.presenceOfHeader(formHeaderID));
146-
147-
// Again, click "Add Subscription" button and verify that "Subscription Form" is open
156+
assert (!subscriptionPage.presenceOfHeader(formHeaderID));
157+
158+
// Again, click "Add Subscription" button and verify that "Subscription
159+
// Form" is open
148160
subscriptionPage.clickAddSubscription();
149161
assert ((new WebDriverWait(driver, 10).until((webdriver) -> subscriptionPage.presenceOfHeader(formHeaderID))));
150-
151-
// On subscription form, select the template as "Mail Trigger" and verify
162+
163+
// On subscription form, select the template as "Mail Trigger" and
164+
// verify
152165
String selectID = "selectTemplate";
153166
String tempMail = "Mail Trigger";
154167
subscriptionPage.selectDropdown(selectID, tempMail);
@@ -157,26 +170,26 @@ public void testSubscription() throws Exception {
157170
assert (new WebDriverWait(driver, 10)
158171
.until((webdriver) -> (subscriptionPage.getValueFromElement().equals("mymail@company.com"))));
159172

160-
// On subscription form, select the template as "REST POST (Raw Body :JSON)"
161-
// and verify
173+
// On subscription form, select the template as "REST POST (Raw
174+
// Body:JSON)" and verify
162175
String tempPost = "REST POST (Raw Body : JSON)";
163176
subscriptionPage.selectDropdown(selectID, tempPost);
164177
assert (new WebDriverWait(driver, 10)
165178
.until((webdriver) -> (subscriptionPage.getValueFromSelect().equals("REST_POST"))));
166179
assert (new WebDriverWait(driver, 10).until(
167180
(webdriver) -> (subscriptionPage.getValueFromElement().equals("http://<MyHost:port>/api/doit"))));
168181

169-
// On subscription form, select the template as "Jenkins Pipeline Parameterized
170-
// Job Trigger" and verify
182+
// On subscription form, select the template as "Jenkins Pipeline
183+
// Parameterized Job Trigger" and verify
171184
String tempJenkins = "Jenkins Pipeline Parameterized Job Trigger";
172185
subscriptionPage.selectDropdown(selectID, tempJenkins);
173186
assertEquals("REST_POST", subscriptionPage.getValueFromSelect());
174187
assertEquals("http://<JenkinsHost:port>/job/<JobName>/job/<branch>/build",
175188
subscriptionPage.getValueFromElement());
176189

177-
// Choose Authorization as "Basic_AUTH" ===> input User Name as "ABCD" and Token
178-
// as "EFGH" ===> click "Generate Key/Value Pair", verify the basic
179-
// authentication is generated
190+
// Choose Authorization as "Basic_AUTH" ===> input User Name as "ABCD"
191+
// and Token as "EFGH" ===> click "Generate Key/Value Pair", verify the
192+
// basic authentication is generated
180193
String selectAuthID = "selectAuth";
181194
String authValue = "BASIC_AUTH";
182195
String userName = "ABCD";
@@ -191,34 +204,37 @@ public void testSubscription() throws Exception {
191204
String requirementFieldID = "requirementID";
192205

193206
subscriptionPage.selectDropdown(selectAuthID, authValue);
194-
207+
195208
subscriptionPage.addFieldValue(userNameID, userName);
196209
subscriptionPage.addFieldValue(tokenID, token);
197210
String kvID = "kvID";
198-
subscriptionPage.clickKVbtn(kvID);
211+
subscriptionPage.clickKVbtn(kvID);
199212
assert (new WebDriverWait(driver, 10).until((webdriver) -> driver.getPageSource().contains("Authorization")));
200-
201-
// Test "Repeat" dropdown: Select repeat value as "true" and then verify the selected value
213+
214+
// Test "Repeat" dropdown: Select repeat value as "true" and then verify
215+
// the selected value
202216
subscriptionPage.selectDropdown(selectRepeatID, repeatValue);
203217
assert (new WebDriverWait(driver, 10)
204-
.until((webdriver) -> (subscriptionPage.getValueFromSelectRepeat().equals(repeatValue))));
205-
206-
// Test "Add Condition" button: click add condition button and check that it adds an additional "condition" field
218+
.until((webdriver) -> (subscriptionPage.getValueFromSelectRepeat().equals(repeatValue))));
219+
220+
// Test "Add Condition" button: click add condition button and check
221+
// that it adds an additional "condition" field
207222
subscriptionPage.clickAddConditionBtn();
208223
assertEquals(2, subscriptionPage.countElements(conditionFieldID));
209-
210-
// Test "Add Requirement" button: click the button and assert that it adds an additional "requirement" field
224+
225+
// Test "Add Requirement" button: click the button and assert that it
226+
// adds an additional "requirement" field
211227
subscriptionPage.clickAddRequirementBtn();
212228
assertEquals(2, subscriptionPage.countElements(requirementFieldID));
213229

214-
// Test save subscription form: add subscription name as
215-
// "selenium_test_subscription" and then click "save" button verification
216-
// that subscription is added in the datatable (and is displayed on the main
217-
// page)
218-
String responseSave = this.getJSONStringFromFile(SUBSCRIPTION_FOR_SAVE_TEST_FILE_PATH);
230+
// Test save subscription form: add subscription name
231+
// as "selenium_test_subscription" and then click "save" button
232+
// verification that subscription is added in the datatable (and is
233+
// displayed on the main page)
234+
String responseSave = getJSONStringFromFile(SUBSCRIPTION_FOR_SAVE_TEST_FILE_PATH);
219235
subscriptionPage.addFieldValue(subNameID, subName);
220236
subscriptionPage.clickFormsSaveBtn(responseSave);
221-
assert (subscriptionPage.textExistsInTable("Selenium_test_subscription"));
237+
assert (subscriptionPage.textExistsInTable("Selenium_test_subscription"));
222238

223239
}
224240
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.io.IOException;
99
import java.util.stream.IntStream;
1010

11-
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertTrue;
1212

1313
public class TestAlarm extends SeleniumBaseClass {
1414

@@ -25,6 +25,6 @@ public void testAlarm() throws IOException {
2525

2626
//Click alarm button few times
2727
IntStream.range(0, 5).forEachOrdered(i -> indexPageObject.clickAlarmButton());
28-
assertEquals(2, driver.findElements(By.className("dropdown-item")).size());
28+
assertTrue(driver.findElements(By.className("dropdown-item")).size() >= 2);
2929
}
3030
}

0 commit comments

Comments
 (0)