2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.config.servlet;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.mielecloud.internal.util.ReflectionUtil.setPrivate;
20 import java.time.LocalDateTime;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.mielecloud.internal.auth.OAuthException;
25 import org.openhab.binding.mielecloud.internal.config.OAuthAuthorizationHandler;
26 import org.openhab.binding.mielecloud.internal.config.exception.NoOngoingAuthorizationException;
27 import org.openhab.binding.mielecloud.internal.config.exception.OngoingAuthorizationException;
28 import org.openhab.binding.mielecloud.internal.util.AbstractConfigFlowTest;
29 import org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants;
30 import org.openhab.binding.mielecloud.internal.util.Website;
33 * @author Björn Lange - Initial Contribution
36 public class ForwardToLoginServletTest extends AbstractConfigFlowTest {
38 public void whenAuthorizationCannotBeBegunThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
41 OAuthAuthorizationHandler authorizationHandler = mock(OAuthAuthorizationHandler.class);
42 doThrow(new OngoingAuthorizationException("", LocalDateTime.now().plusMinutes(3))).when(authorizationHandler)
43 .beginAuthorization(anyString(), anyString(), any(), anyString());
44 setPrivate(getForwardToLoginServlet(), "authorizationHandler", authorizationHandler);
47 Website maybePairAccountSite = getCrawler().doGetRelative("/mielecloud/forwardToLogin?"
48 + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "="
49 + MieleCloudBindingIntegrationTestConstants.CLIENT_ID + "&"
50 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "="
51 + MieleCloudBindingIntegrationTestConstants.CLIENT_SECRET + "&"
52 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "="
53 + MieleCloudBindingIntegrationTestConstants.BRIDGE_ID + "&" + ForwardToLoginServlet.EMAIL_PARAMETER_NAME
54 + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
57 assertTrue(maybePairAccountSite.contains(
58 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
59 assertTrue(maybePairAccountSite.contains(
60 "There is an authorization ongoing at the moment. Please complete that authorization prior to starting a new one or try again"));
64 public void whenNoAuthorizationIsOngoingWhenTheAuthorizationUrlIsRequestedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
67 OAuthAuthorizationHandler authorizationHandler = mock(OAuthAuthorizationHandler.class);
68 doThrow(new NoOngoingAuthorizationException("")).when(authorizationHandler).getAuthorizationUrl(anyString());
69 setPrivate(getForwardToLoginServlet(), "authorizationHandler", authorizationHandler);
72 Website maybePairAccountSite = getCrawler().doGetRelative("/mielecloud/forwardToLogin?"
73 + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "="
74 + MieleCloudBindingIntegrationTestConstants.CLIENT_ID + "&"
75 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "="
76 + MieleCloudBindingIntegrationTestConstants.CLIENT_SECRET + "&"
77 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "="
78 + MieleCloudBindingIntegrationTestConstants.BRIDGE_ID + "&" + ForwardToLoginServlet.EMAIL_PARAMETER_NAME
79 + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
82 assertTrue(maybePairAccountSite.contains(
83 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
84 assertTrue(maybePairAccountSite.contains(
85 "Failed to start auhtorization process. Are you trying to perform multiple authorizations at the same time?"));
89 public void whenNoClientIdIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed() throws Exception {
91 Website maybePairAccountSite = getCrawler().doGetRelative("/mielecloud/forwardToLogin?"
92 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "="
93 + MieleCloudBindingIntegrationTestConstants.CLIENT_SECRET + "&"
94 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "="
95 + MieleCloudBindingIntegrationTestConstants.BRIDGE_ID + "&" + ForwardToLoginServlet.EMAIL_PARAMETER_NAME
96 + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
99 assertTrue(maybePairAccountSite.contains(
100 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
101 assertTrue(maybePairAccountSite.contains("Missing client ID."));
105 public void whenAnEmptyClientIdIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
108 Website maybePairAccountSite = getCrawler().doGetRelative("/mielecloud/forwardToLogin?"
109 + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "=&"
110 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "="
111 + MieleCloudBindingIntegrationTestConstants.CLIENT_SECRET + "&"
112 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "="
113 + MieleCloudBindingIntegrationTestConstants.BRIDGE_ID + "&" + ForwardToLoginServlet.EMAIL_PARAMETER_NAME
114 + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
117 assertTrue(maybePairAccountSite.contains(
118 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
119 assertTrue(maybePairAccountSite.contains("Missing client ID."));
123 public void whenNoClientSecretIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
126 Website maybePairAccountSite = getCrawler().doGetRelative("/mielecloud/forwardToLogin?"
127 + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "="
128 + MieleCloudBindingIntegrationTestConstants.CLIENT_ID + "&"
129 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "="
130 + MieleCloudBindingIntegrationTestConstants.BRIDGE_ID + "&" + ForwardToLoginServlet.EMAIL_PARAMETER_NAME
131 + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
134 assertTrue(maybePairAccountSite.contains(
135 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
136 assertTrue(maybePairAccountSite.contains("Missing client secret."));
140 public void whenAnEmptyClientSecretIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
143 Website maybePairAccountSite = getCrawler().doGetRelative("/mielecloud/forwardToLogin?"
144 + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "="
145 + MieleCloudBindingIntegrationTestConstants.CLIENT_ID + "&"
146 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "=" + "&"
147 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "="
148 + MieleCloudBindingIntegrationTestConstants.BRIDGE_ID + "&" + ForwardToLoginServlet.EMAIL_PARAMETER_NAME
149 + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
152 assertTrue(maybePairAccountSite.contains(
153 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
154 assertTrue(maybePairAccountSite.contains("Missing client secret."));
158 public void whenOAuthClientDoesNotProvideAnAuthorizationUrlThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
161 OAuthAuthorizationHandler authorizationHandler = mock(OAuthAuthorizationHandler.class);
162 doThrow(new OAuthException("")).when(authorizationHandler).getAuthorizationUrl(anyString());
163 setPrivate(getForwardToLoginServlet(), "authorizationHandler", authorizationHandler);
166 Website maybePairAccountSite = getCrawler().doGetRelative("/mielecloud/forwardToLogin?"
167 + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "="
168 + MieleCloudBindingIntegrationTestConstants.CLIENT_ID + "&"
169 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "="
170 + MieleCloudBindingIntegrationTestConstants.CLIENT_SECRET + "&"
171 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "="
172 + MieleCloudBindingIntegrationTestConstants.BRIDGE_ID + "&" + ForwardToLoginServlet.EMAIL_PARAMETER_NAME
173 + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
176 assertTrue(maybePairAccountSite.contains(
177 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
178 assertTrue(maybePairAccountSite.contains("Failed to derive redirect URL."));
182 public void whenNoBridgeUidIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
185 Website maybePairAccountSite = getCrawler().doGetRelative("/mielecloud/forwardToLogin?"
186 + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "="
187 + MieleCloudBindingIntegrationTestConstants.CLIENT_ID + "&"
188 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "="
189 + MieleCloudBindingIntegrationTestConstants.CLIENT_SECRET + "&"
190 + ForwardToLoginServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
193 assertTrue(maybePairAccountSite.contains(
194 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
195 assertTrue(maybePairAccountSite.contains("Missing bridge ID."));
199 public void whenAnEmptyBridgeUidIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
202 Website maybePairAccountSite = getCrawler().doGetRelative("/mielecloud/forwardToLogin?"
203 + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "="
204 + MieleCloudBindingIntegrationTestConstants.CLIENT_ID + "&"
205 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "="
206 + MieleCloudBindingIntegrationTestConstants.CLIENT_SECRET + "&"
207 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "=" + "&"
208 + ForwardToLoginServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
211 assertTrue(maybePairAccountSite.contains(
212 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
213 assertTrue(maybePairAccountSite.contains("Missing bridge ID."));
217 public void whenAMalformedBridgeUidIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
220 Website maybePairAccountSite = getCrawler().doGetRelative("/mielecloud/forwardToLogin?"
221 + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "="
222 + MieleCloudBindingIntegrationTestConstants.CLIENT_ID + "&"
223 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "="
224 + MieleCloudBindingIntegrationTestConstants.CLIENT_SECRET + "&"
225 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "=genesis!" + "&"
226 + ForwardToLoginServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
229 assertTrue(maybePairAccountSite.contains(
230 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
231 assertTrue(maybePairAccountSite
232 .contains("Malformed bridge ID. A bridge ID may only contain letters, numbers, '-' and '_'!"));
236 public void whenNoEmailIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed() throws Exception {
238 Website maybePairAccountSite = getCrawler()
239 .doGetRelative("/mielecloud/forwardToLogin?" + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "="
240 + MieleCloudBindingIntegrationTestConstants.CLIENT_ID + "&"
241 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "="
242 + MieleCloudBindingIntegrationTestConstants.CLIENT_SECRET + "&"
243 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "="
244 + MieleCloudBindingIntegrationTestConstants.BRIDGE_ID);
247 assertTrue(maybePairAccountSite.contains(
248 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
249 assertTrue(maybePairAccountSite.contains("Missing e-mail address."));
253 public void whenAnEmptyEmailIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
256 Website maybePairAccountSite = getCrawler()
257 .doGetRelative("/mielecloud/forwardToLogin?" + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "="
258 + MieleCloudBindingIntegrationTestConstants.CLIENT_ID + "&"
259 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "="
260 + MieleCloudBindingIntegrationTestConstants.CLIENT_SECRET + "&"
261 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "="
262 + MieleCloudBindingIntegrationTestConstants.BRIDGE_ID + "&"
263 + ForwardToLoginServlet.EMAIL_PARAMETER_NAME + "=");
266 assertTrue(maybePairAccountSite.contains(
267 "Go to <a href=\"https://www.miele.com/f/com/en/register_api.aspx\">the Miele developer portal</a> to obtain your"));
268 assertTrue(maybePairAccountSite.contains("Missing e-mail address."));