]> git.basschouten.com Git - openhab-addons.git/blob
a7a2c89c6a7568072215694adfb29d8d923c6dc3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.config.servlet;
14
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;
19
20 import java.time.LocalDateTime;
21
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;
31
32 /**
33  * @author Björn Lange - Initial Contribution
34  */
35 @NonNullByDefault
36 public class ForwardToLoginServletTest extends AbstractConfigFlowTest {
37     @Test
38     public void whenAuthorizationCannotBeBegunThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
39             throws Exception {
40         // given:
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);
45
46         // when:
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);
55
56         // then:
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"));
61     }
62
63     @Test
64     public void whenNoAuthorizationIsOngoingWhenTheAuthorizationUrlIsRequestedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
65             throws Exception {
66         // given:
67         OAuthAuthorizationHandler authorizationHandler = mock(OAuthAuthorizationHandler.class);
68         doThrow(new NoOngoingAuthorizationException("")).when(authorizationHandler).getAuthorizationUrl(anyString());
69         setPrivate(getForwardToLoginServlet(), "authorizationHandler", authorizationHandler);
70
71         // when:
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);
80
81         // then:
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?"));
86     }
87
88     @Test
89     public void whenNoClientIdIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed() throws Exception {
90         // when:
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);
97
98         // then:
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."));
102     }
103
104     @Test
105     public void whenAnEmptyClientIdIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
106             throws Exception {
107         // when:
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);
115
116         // then:
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."));
120     }
121
122     @Test
123     public void whenNoClientSecretIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
124             throws Exception {
125         // when:
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);
132
133         // then:
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."));
137     }
138
139     @Test
140     public void whenAnEmptyClientSecretIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
141             throws Exception {
142         // when:
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);
150
151         // then:
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."));
155     }
156
157     @Test
158     public void whenOAuthClientDoesNotProvideAnAuthorizationUrlThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
159             throws Exception {
160         // given:
161         OAuthAuthorizationHandler authorizationHandler = mock(OAuthAuthorizationHandler.class);
162         doThrow(new OAuthException("")).when(authorizationHandler).getAuthorizationUrl(anyString());
163         setPrivate(getForwardToLoginServlet(), "authorizationHandler", authorizationHandler);
164
165         // when:
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);
174
175         // then:
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."));
179     }
180
181     @Test
182     public void whenNoBridgeUidIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
183             throws Exception {
184         // when:
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);
191
192         // then:
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."));
196     }
197
198     @Test
199     public void whenAnEmptyBridgeUidIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
200             throws Exception {
201         // when:
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);
209
210         // then:
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."));
214     }
215
216     @Test
217     public void whenAMalformedBridgeUidIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
218             throws Exception {
219         // when:
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);
227
228         // then:
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 '_'!"));
233     }
234
235     @Test
236     public void whenNoEmailIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed() throws Exception {
237         // when:
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);
245
246         // then:
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."));
250     }
251
252     @Test
253     public void whenAnEmptyEmailIsPassedThenTheBrowserIsRedirectedToThePairSiteAndAWarningIsDisplayed()
254             throws Exception {
255         // when:
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 + "=");
264
265         // then:
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."));
269     }
270 }