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