]> git.basschouten.com Git - openhab-addons.git/blob
c7f3bfae0fc514585fc7d2d6c6185c8f358dbbfb
[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.io.hueemulation.internal.rest;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18
19 import java.io.IOException;
20 import java.util.Collections;
21 import java.util.Dictionary;
22
23 import org.eclipse.jetty.client.api.ContentResponse;
24 import org.glassfish.jersey.server.ResourceConfig;
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.mockito.Mockito;
29 import org.openhab.io.hueemulation.internal.ConfigStore;
30 import org.openhab.io.hueemulation.internal.HueEmulationConfig;
31 import org.openhab.io.hueemulation.internal.dto.HueUnauthorizedConfig;
32 import org.openhab.io.hueemulation.internal.dto.response.HueResponse;
33 import org.openhab.io.hueemulation.internal.dto.response.HueSuccessResponseCreateUser;
34 import org.openhab.io.hueemulation.internal.rest.mocks.ConfigStoreWithoutMetadata;
35
36 import com.google.gson.Gson;
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonParser;
39
40 /**
41  * Tests for various user management API endpoints.
42  *
43  * @author David Graeff - Initial contribution
44  */
45 public class UsersAndConfigTests {
46
47     ConfigurationAccess configurationAccess = new ConfigurationAccess();
48
49     CommonSetup commonSetup;
50
51     @BeforeEach
52     public void setUp() throws IOException {
53         commonSetup = new CommonSetup(false);
54
55         configurationAccess.cs = commonSetup.cs;
56         configurationAccess.userManagement = commonSetup.userManagement;
57         configurationAccess.configAdmin = commonSetup.configAdmin;
58
59         commonSetup.start(new ResourceConfig().registerInstances(configurationAccess));
60     }
61
62     @AfterEach
63     public void tearDown() throws Exception {
64         commonSetup.dispose();
65     }
66
67     @Test
68     public void invalidUser() {
69         assertFalse(commonSetup.userManagement.authorizeUser("blub"));
70     }
71
72     @Test
73     public void validUser() {
74         assertTrue(commonSetup.userManagement.authorizeUser("testuser"));
75     }
76
77     @Test
78     public void configStoreRestartOnNoUUID() {
79         ConfigStore configStore = new ConfigStoreWithoutMetadata(commonSetup.networkAddressService,
80                 commonSetup.configAdmin, commonSetup.scheduler);
81
82         // No uuid known yet
83         assertThat(configStore.ds.config.uuid, is(""));
84         configStore.activate(Collections.emptyMap());
85
86         assertThat(configStore.getConfig().uuid, not(is("")));
87         // The config admin service was requested for the service config
88         Mockito.verify(commonSetup.configAdminConfig).getProperties();
89         Dictionary<String, Object> p = commonSetup.configAdminConfig.getProperties();
90         // And the service config was updated
91         assertThat(p.get(HueEmulationConfig.CONFIG_UUID), is(configStore.getConfig().uuid));
92     }
93
94     @Test
95     public void addUser() throws Exception {
96         // GET should fail
97         assertEquals(405, commonSetup.sendGet().getStatus());
98
99         String body = "{'username':'testuser','devicetype':'app#device'}";
100
101         // Post should create a user, except: if linkbutton not enabled
102         ContentResponse response = commonSetup.sendPost(body);
103         assertThat(response.getStatus(), is(200));
104         HueResponse[] r = commonSetup.cs.gson.fromJson(response.getContentAsString(), HueResponse[].class);
105         assertNotNull(r[0].error);
106
107         // Post should create a user
108         commonSetup.cs.ds.config.linkbutton = true;
109         response = commonSetup.sendPost(body);
110         assertThat(response.getStatus(), is(200));
111
112         JsonElement e = JsonParser.parseString(response.getContentAsString()).getAsJsonArray().get(0);
113         e = e.getAsJsonObject().get("success");
114         HueSuccessResponseCreateUser rc = commonSetup.cs.gson.fromJson(e, HueSuccessResponseCreateUser.class);
115         assertNotNull(rc);
116         assertThat(commonSetup.cs.ds.config.whitelist.get(rc.username).name, is("app#device"));
117     }
118
119     @Test
120     public void unauthorizedAccessTest() throws Exception {
121         // Unauthorized config
122         ContentResponse response = commonSetup.sendGet("/config");
123         assertThat(response.getStatus(), is(200));
124         HueUnauthorizedConfig config = new Gson().fromJson(response.getContentAsString(), HueUnauthorizedConfig.class);
125         assertThat(config.bridgeid, is(commonSetup.cs.ds.config.bridgeid));
126         assertThat(config.name, is(commonSetup.cs.ds.config.name));
127
128         // Invalid user name
129         response = commonSetup.sendGet("/invalid/config");
130         assertThat(response.getStatus(), is(403));
131         assertThat(response.getContentAsString(), containsString("error"));
132     }
133 }