]> git.basschouten.com Git - openhab-addons.git/blob
01fb70d6f940386849f4be5cc07f2ab8cdd04389
[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.anyString;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.mielecloud.internal.util.ReflectionUtil.setPrivate;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.mielecloud.internal.auth.OAuthException;
23 import org.openhab.binding.mielecloud.internal.config.OAuthAuthorizationHandler;
24 import org.openhab.binding.mielecloud.internal.config.exception.NoOngoingAuthorizationException;
25 import org.openhab.binding.mielecloud.internal.util.AbstractConfigFlowTest;
26 import org.openhab.binding.mielecloud.internal.util.Website;
27
28 /**
29  * @author Björn Lange - Initial Contribution
30  */
31 @NonNullByDefault
32 public class ResultServletTest extends AbstractConfigFlowTest {
33     @Test
34     public void whenOAuthErrorAccessDeniedIsReturnedByMieleServiceThenTheFailurePageWithAccordingErrorMessageIsDisplayed()
35             throws Exception {
36         // when:
37         Website website = getCrawler().doGetRelative("/mielecloud/result?" + ResultServlet.ERROR_PARAMETER_NAME + "="
38                 + FailureServlet.OAUTH2_ERROR_ACCESS_DENIED);
39
40         // then:
41         assertTrue(website.contains("Pairing failed!"));
42         assertTrue(website.contains("OAuth2 authentication with Miele cloud service failed: Access denied."));
43     }
44
45     @Test
46     public void whenOAuthErrorInvalidRequestIsReturnedByMieleServiceThenTheFailurePageWithAccordingErrorMessageIsDisplayed()
47             throws Exception {
48         // when:
49         Website website = getCrawler().doGetRelative("/mielecloud/result?" + ResultServlet.ERROR_PARAMETER_NAME + "="
50                 + FailureServlet.OAUTH2_ERROR_INVALID_REQUEST);
51
52         // then:
53         assertTrue(website.contains("Pairing failed!"));
54         assertTrue(website.contains("OAuth2 authentication with Miele cloud service failed: Malformed request."));
55     }
56
57     @Test
58     public void whenOAuthErrorUnauthorizedClientIsReturnedByMieleServiceThenTheFailurePageWithAccordingErrorMessageIsDisplayed()
59             throws Exception {
60         // when:
61         Website website = getCrawler().doGetRelative("/mielecloud/result?" + ResultServlet.ERROR_PARAMETER_NAME + "="
62                 + FailureServlet.OAUTH2_ERROR_UNAUTHORIZED_CLIENT);
63
64         // then:
65         assertTrue(website.contains("Pairing failed!"));
66         assertTrue(website.contains(
67                 "OAuth2 authentication with Miele cloud service failed: Account not authorized to request authorization code."));
68     }
69
70     @Test
71     public void whenOAuthErrorUnsupportedResponseTypeIsReturnedByMieleServiceThenTheFailurePageWithAccordingErrorMessageIsDisplayed()
72             throws Exception {
73         // when:
74         Website website = getCrawler().doGetRelative("/mielecloud/result?" + ResultServlet.ERROR_PARAMETER_NAME + "="
75                 + FailureServlet.OAUTH2_ERROR_UNSUPPORTED_RESPONSE_TYPE);
76
77         // then:
78         assertTrue(website.contains("Pairing failed!"));
79         assertTrue(website.contains(
80                 "OAuth2 authentication with Miele cloud service failed: Obtaining an authorization code is not supported."));
81     }
82
83     @Test
84     public void whenOAuthErrorInvalidScopeIsReturnedByMieleServiceThenTheFailurePageWithAccordingErrorMessageIsDisplayed()
85             throws Exception {
86         // when:
87         Website website = getCrawler().doGetRelative("/mielecloud/result?" + ResultServlet.ERROR_PARAMETER_NAME + "="
88                 + FailureServlet.OAUTH2_ERROR_INVALID_SCOPE);
89
90         // then:
91         assertTrue(website.contains("Pairing failed!"));
92         assertTrue(website.contains("OAuth2 authentication with Miele cloud service failed: Invalid scope."));
93     }
94
95     @Test
96     public void whenOAuthErrorServerErrorIsReturnedByMieleServiceThenTheFailurePageWithAccordingErrorMessageIsDisplayed()
97             throws Exception {
98         // when:
99         Website website = getCrawler().doGetRelative("/mielecloud/result?" + ResultServlet.ERROR_PARAMETER_NAME + "="
100                 + FailureServlet.OAUTH2_ERROR_SERVER_ERROR);
101
102         // then:
103         assertTrue(website.contains("Pairing failed!"));
104         assertTrue(website.contains("OAuth2 authentication with Miele cloud service failed: Unexpected server error."));
105     }
106
107     @Test
108     public void whenOAuthErrorTemporarilyUnavailableIsReturnedByMieleServiceThenTheFailurePageWithAccordingErrorMessageIsDisplayed()
109             throws Exception {
110         // when:
111         Website website = getCrawler().doGetRelative("/mielecloud/result?" + ResultServlet.ERROR_PARAMETER_NAME + "="
112                 + FailureServlet.OAUTH2_ERROR_TEMPORARY_UNAVAILABLE);
113
114         // then:
115         assertTrue(website.contains("Pairing failed!"));
116         assertTrue(website.contains(
117                 "OAuth2 authentication with Miele cloud service failed: Authorization server temporarily unavailable."));
118     }
119
120     @Test
121     public void whenUnknwonOAuthErrorIsReturnedByMieleServiceThenTheFailurePageWithAccordingErrorMessageIsDisplayed()
122             throws Exception {
123         // when:
124         Website website = getCrawler()
125                 .doGetRelative("/mielecloud/result?" + ResultServlet.ERROR_PARAMETER_NAME + "=unknown_oauth_2_error");
126
127         // then:
128         assertTrue(website.contains("Pairing failed!"));
129         assertTrue(website.contains(
130                 "OAuth2 authentication with Miele cloud service failed: Unknown error code \"unknown_oauth_2_error\"."));
131     }
132
133     @Test
134     public void whenCodeParameterIsNotPassedByMieleServiceThenTheFailurePageWithAccordingErrorMessageIsDisplayed()
135             throws Exception {
136         // when:
137         Website website = getCrawler()
138                 .doGetRelative("/mielecloud/result?" + ResultServlet.STATE_PARAMETER_NAME + "=state");
139
140         // then:
141         assertTrue(website.contains("Pairing failed!"));
142         assertTrue(website.contains("Miele cloud service returned an illegal response."));
143     }
144
145     @Test
146     public void whenStateParameterIsNotPassedByMieleServiceThenTheFailurePageWithAccordingErrorMessageIsDisplayed()
147             throws Exception {
148         // when:
149         Website website = getCrawler()
150                 .doGetRelative("/mielecloud/result?" + ResultServlet.CODE_PARAMETER_NAME + "=code");
151
152         // then:
153         assertTrue(website.contains("Pairing failed!"));
154         assertTrue(website.contains("Miele cloud service returned an illegal response."));
155     }
156
157     @Test
158     public void whenNoAuthorizationIsOngoingThenTheFailurePageWithAccordingErrorMessageIsDisplayed() throws Exception {
159         // given:
160         OAuthAuthorizationHandler authorizationHandler = mock(OAuthAuthorizationHandler.class);
161         doThrow(new NoOngoingAuthorizationException("")).when(authorizationHandler).completeAuthorization(anyString());
162         setPrivate(getResultServlet(), "authorizationHandler", authorizationHandler);
163
164         // when:
165         Website website = getCrawler().doGetRelative("/mielecloud/result?" + ResultServlet.CODE_PARAMETER_NAME
166                 + "=code&" + ResultServlet.STATE_PARAMETER_NAME + "=state");
167
168         // then:
169         assertTrue(website.contains("Pairing failed!"));
170         assertTrue(website.contains("There is no ongoing authorization. Please start an authorization first."));
171     }
172
173     @Test
174     public void whenLastStepOfAuthorizationFailsThenTheFailurePageWithAccordingErrorMessageIsDisplayed()
175             throws Exception {
176         // given:
177         OAuthAuthorizationHandler authorizationHandler = mock(OAuthAuthorizationHandler.class);
178         doThrow(new OAuthException("")).when(authorizationHandler).completeAuthorization(anyString());
179         setPrivate(getResultServlet(), "authorizationHandler", authorizationHandler);
180
181         // when:
182         Website website = getCrawler().doGetRelative("/mielecloud/result?" + ResultServlet.CODE_PARAMETER_NAME
183                 + "=code&" + ResultServlet.STATE_PARAMETER_NAME + "=state");
184
185         // then:
186         assertTrue(website.contains("Pairing failed!"));
187         assertTrue(website
188                 .contains("Completing the final authorization request failed. Please try the config flow again."));
189     }
190 }