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.io.hueemulation.internal.rest;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
19 import java.io.IOException;
20 import java.util.Collections;
21 import java.util.Dictionary;
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;
36 import com.google.gson.Gson;
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonParser;
41 * Tests for various user management API endpoints.
43 * @author David Graeff - Initial contribution
45 public class UsersAndConfigTests {
47 ConfigurationAccess configurationAccess = new ConfigurationAccess();
49 CommonSetup commonSetup;
52 public void setUp() throws IOException {
53 commonSetup = new CommonSetup(false);
55 configurationAccess.cs = commonSetup.cs;
56 configurationAccess.userManagement = commonSetup.userManagement;
57 configurationAccess.configAdmin = commonSetup.configAdmin;
59 commonSetup.start(new ResourceConfig().registerInstances(configurationAccess));
63 public void tearDown() throws Exception {
64 commonSetup.dispose();
68 public void invalidUser() {
69 assertFalse(commonSetup.userManagement.authorizeUser("blub"));
73 public void validUser() {
74 assertTrue(commonSetup.userManagement.authorizeUser("testuser"));
78 public void configStoreRestartOnNoUUID() {
79 ConfigStore configStore = new ConfigStoreWithoutMetadata(commonSetup.networkAddressService,
80 commonSetup.configAdmin, commonSetup.scheduler);
83 assertThat(configStore.ds.config.uuid, is(""));
84 configStore.activate(Collections.emptyMap());
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));
95 public void addUser() throws Exception {
97 assertEquals(405, commonSetup.sendGet().getStatus());
99 String body = "{'username':'testuser','devicetype':'app#device'}";
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);
107 // Post should create a user
108 commonSetup.cs.ds.config.linkbutton = true;
109 response = commonSetup.sendPost(body);
110 assertThat(response.getStatus(), is(200));
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);
116 assertThat(commonSetup.cs.ds.config.whitelist.get(rc.username).name, is("app#device"));
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));
129 response = commonSetup.sendGet("/invalid/config");
130 assertThat(response.getStatus(), is(403));
131 assertThat(response.getContentAsString(), containsString("error"));