Skip to content

Commit 4c6c6cf

Browse files
committed
Init username and password as global vars in huego_test.go
1 parent 2fedcda commit 4c6c6cf

13 files changed

+117
-135
lines changed

bridge_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package huego_test
22

33
import (
44
"github.com/amimof/huego"
5-
"os"
5+
"strings"
66
"testing"
77
)
88

99
func TestLogin(t *testing.T) {
10-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_PASSWORD"))
10+
b := huego.New(hostname, username)
1111
c, err := b.GetConfig()
1212
if err != nil {
1313
t.Fatal(err)
@@ -17,16 +17,13 @@ func TestLogin(t *testing.T) {
1717
}
1818

1919
func TestLoginUnauthorized(t *testing.T) {
20-
b := huego.New(os.Getenv("HUE_HOSTNAME"), "")
20+
b := huego.New(hostname, "")
2121
b = b.Login("invalid_password")
2222
_, err := b.GetLights()
2323
if err != nil {
24-
t.Fatal(err)
25-
/*
26-
We should do something here to check if the user is allowed
27-
the requested resource (in this case Config). A simple err
28-
shouldn't fail the test.
29-
*/
24+
if strings.Contains(err.Error(), "unauthorized user") == false {
25+
t.Fatal(err)
26+
}
3027
}
3128
t.Logf("Bridge: %s, Username: %s", b.Host, b.User)
3229
t.Log("User logged in and authenticated which isn't what we want")

capabilities_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package huego_test
22

33
import (
44
"github.com/amimof/huego"
5-
"os"
65
"testing"
76
)
87

98
func TestGetCapabilities(t *testing.T) {
10-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
9+
b := huego.New(hostname, username)
1110
c, err := b.GetCapabilities()
1211
if err != nil {
1312
t.Fatal(c)

config.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package huego
33
// Config holds the bridge hardware configuration
44
type Config struct {
55
Name string `json:"name,omitempty"`
6-
SwUpdate *SwUpdate `json:"swupdate"`
7-
SwUpdate2 *SwUpdate2 `json:"swupdate2"`
6+
SwUpdate SwUpdate `json:"swupdate"`
7+
SwUpdate2 SwUpdate2 `json:"swupdate2"`
88
WhitelistMap map[string]Whitelist `json:"whitelist"`
99
Whitelist []Whitelist `json:"-"`
10-
PortalState *PortalState `json:"portalstate"`
10+
PortalState PortalState `json:"portalstate"`
1111
APIVersion string `json:"apiversion,omitempty"`
1212
SwVersion string `json:"swversion,omitempty"`
1313
ProxyAddress string `json:"proxyaddress,omitempty"`
@@ -29,28 +29,28 @@ type Config struct {
2929
ReplacesBridgeID string `json:"replacesbridgeid,omitempty"`
3030
DatastoreVersion string `json:"datastoreversion,omitempty"`
3131
StarterKitID string `json:"starterkitid,omitempty"`
32-
InternetService *InternetService `json:"internetservices,omitempty"`
32+
InternetService InternetService `json:"internetservices,omitempty"`
3333
}
3434

3535
// SwUpdate contains information related to software updates. Deprecated in 1.20
3636
type SwUpdate struct {
37-
CheckForUpdate bool `json:"checkforupdate,omitempty"`
38-
DeviceTypes *DeviceTypes `json:"devicetypes"`
39-
UpdateState uint8 `json:"updatestate,omitempty"`
40-
Notify bool `json:"notify,omitempty"`
41-
URL string `json:"url,omitempty"`
42-
Text string `json:"text,omitempty"`
37+
CheckForUpdate bool `json:"checkforupdate,omitempty"`
38+
DeviceTypes DeviceTypes `json:"devicetypes"`
39+
UpdateState uint8 `json:"updatestate,omitempty"`
40+
Notify bool `json:"notify,omitempty"`
41+
URL string `json:"url,omitempty"`
42+
Text string `json:"text,omitempty"`
4343
}
4444

4545
// SwUpdate2 contains information related to software updates
4646
type SwUpdate2 struct {
47-
Bridge *BridgeConfig `json:"bridge"`
48-
CheckForUpdate bool `json:"checkforupdate,omitempty"`
49-
State string `json:"state,omitempty"`
50-
Install bool `json:"install,omitempty"`
51-
AutoInstall *AutoInstall `json:"autoinstall"`
52-
LastChange string `json:"lastchange,omitempty"`
53-
LastInstall string `json:"lastinstall,omitempty"`
47+
Bridge BridgeConfig `json:"bridge"`
48+
CheckForUpdate bool `json:"checkforupdate,omitempty"`
49+
State string `json:"state,omitempty"`
50+
Install bool `json:"install,omitempty"`
51+
AutoInstall AutoInstall `json:"autoinstall"`
52+
LastChange string `json:"lastchange,omitempty"`
53+
LastInstall string `json:"lastinstall,omitempty"`
5454
}
5555

5656
// DeviceTypes details the type of updates available
@@ -106,7 +106,7 @@ type PortalState struct {
106106
type Datastore struct {
107107
Lights []Light `json:"lights"`
108108
Groups []Group `json:"groups"`
109-
Config *Config `json:"config"`
109+
Config Config `json:"config"`
110110
Schedules []Schedule `json:"schedules"`
111111
Scenes []Scene `json:"scenes"`
112112
Sensors []Sensor `json:"sensors"`

config_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package huego_test
22

33
import (
44
"github.com/amimof/huego"
5-
"os"
65
"testing"
76
)
87

98
func TestGetConfig(t *testing.T) {
10-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
9+
b := huego.New(hostname, username)
1110
config, err := b.GetConfig()
1211
if err != nil {
1312
t.Fatal(err)
@@ -65,7 +64,7 @@ func TestGetConfig(t *testing.T) {
6564
}
6665

6766
func TestCreateUser(t *testing.T) {
68-
b := huego.New(os.Getenv("HUE_HOSTNAME"), "")
67+
b := huego.New(hostname, "")
6968
u, err := b.CreateUser("github.com/amimof/huego#go test")
7069
if err != nil {
7170
t.Fatal(err)
@@ -75,7 +74,7 @@ func TestCreateUser(t *testing.T) {
7574
}
7675

7776
func TestGetUsers(t *testing.T) {
78-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
77+
b := huego.New(hostname, username)
7978
users, err := b.GetUsers()
8079
if err != nil {
8180
t.Fatal(err)
@@ -90,13 +89,13 @@ func TestGetUsers(t *testing.T) {
9089
}
9190

9291
func TestDeleteUser(t *testing.T) {
93-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
92+
b := huego.New(hostname, username)
9493
users, err := b.GetUsers()
9594
if err != nil {
9695
t.Fatal(err)
9796
}
9897
for _, user := range users {
99-
if user.Name == "huego#tests" {
98+
if user.Name == "github.com/amimof/huego#go test" {
10099
err := b.DeleteUser(user.Username)
101100
if err != nil {
102101
t.Fatal(err)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module github.com/amimof/huego

group_test.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package huego_test
22

33
import (
44
"github.com/amimof/huego"
5-
"os"
65
"testing"
76
)
87

98
func TestGetGroups(t *testing.T) {
10-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
9+
b := huego.New(hostname, username)
1110
groups, err := b.GetGroups()
1211
if err != nil {
1312
t.Fatal(err)
@@ -45,7 +44,7 @@ func TestGetGroups(t *testing.T) {
4544
}
4645

4746
func TestGetGroup(t *testing.T) {
48-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
47+
b := huego.New(hostname, username)
4948
groups, err := b.GetGroups()
5049
if err != nil {
5150
t.Fatal(err)
@@ -86,7 +85,7 @@ func TestGetGroup(t *testing.T) {
8685
}
8786

8887
func TestCreateGroup(t *testing.T) {
89-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
88+
b := huego.New(hostname, username)
9089
resp, err := b.CreateGroup(huego.Group{
9190
Name: "TestGroup",
9291
Type: "Room",
@@ -104,7 +103,7 @@ func TestCreateGroup(t *testing.T) {
104103
}
105104

106105
func TestUpdateGroup(t *testing.T) {
107-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
106+
b := huego.New(hostname, username)
108107
id := 3
109108
resp, err := b.UpdateGroup(id, huego.Group{
110109
Name: "TestGroup (Updated)",
@@ -121,7 +120,7 @@ func TestUpdateGroup(t *testing.T) {
121120
}
122121

123122
func TestSetGroupState(t *testing.T) {
124-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
123+
b := huego.New(hostname, username)
125124
id := 3
126125
resp, err := b.SetGroupState(id, huego.State{
127126
On: true,
@@ -139,7 +138,7 @@ func TestSetGroupState(t *testing.T) {
139138
}
140139

141140
func TestRenameGroup(t *testing.T) {
142-
bridge := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
141+
bridge := huego.New(hostname, username)
143142
id := 3
144143
group, err := bridge.GetGroup(id)
145144
if err != nil {
@@ -153,7 +152,7 @@ func TestRenameGroup(t *testing.T) {
153152
}
154153

155154
func TestTurnOffGroup(t *testing.T) {
156-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
155+
b := huego.New(hostname, username)
157156
id := 1
158157
group, err := b.GetGroup(id)
159158
if err != nil {
@@ -168,7 +167,7 @@ func TestTurnOffGroup(t *testing.T) {
168167
}
169168

170169
func TestTurnOnGroup(t *testing.T) {
171-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
170+
b := huego.New(hostname, username)
172171
id := 1
173172
group, err := b.GetGroup(id)
174173
if err != nil {
@@ -183,7 +182,7 @@ func TestTurnOnGroup(t *testing.T) {
183182
}
184183

185184
func TestIfGroupIsOn(t *testing.T) {
186-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
185+
b := huego.New(hostname, username)
187186
id := 1
188187
group, err := b.GetGroup(id)
189188
if err != nil {
@@ -193,7 +192,7 @@ func TestIfGroupIsOn(t *testing.T) {
193192
}
194193

195194
func TestSetGroupBri(t *testing.T) {
196-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
195+
b := huego.New(hostname, username)
197196
id := 1
198197
group, err := b.GetGroup(id)
199198
if err != nil {
@@ -207,7 +206,7 @@ func TestSetGroupBri(t *testing.T) {
207206
}
208207

209208
func TestSetGroupHue(t *testing.T) {
210-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
209+
b := huego.New(hostname, username)
211210
id := 1
212211
group, err := b.GetGroup(id)
213212
if err != nil {
@@ -221,7 +220,7 @@ func TestSetGroupHue(t *testing.T) {
221220
}
222221

223222
func TestSetGroupSat(t *testing.T) {
224-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
223+
b := huego.New(hostname, username)
225224
id := 1
226225
group, err := b.GetGroup(id)
227226
if err != nil {
@@ -235,7 +234,7 @@ func TestSetGroupSat(t *testing.T) {
235234
}
236235

237236
func TestSetGroupXy(t *testing.T) {
238-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
237+
b := huego.New(hostname, username)
239238
id := 1
240239
group, err := b.GetGroup(id)
241240
if err != nil {
@@ -249,7 +248,7 @@ func TestSetGroupXy(t *testing.T) {
249248
}
250249

251250
func TestSetGroupCt(t *testing.T) {
252-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
251+
b := huego.New(hostname, username)
253252
id := 1
254253
group, err := b.GetGroup(id)
255254
if err != nil {
@@ -263,7 +262,7 @@ func TestSetGroupCt(t *testing.T) {
263262
}
264263

265264
func TestSetGroupScene(t *testing.T) {
266-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
265+
b := huego.New(hostname, username)
267266
id := 1
268267
group, err := b.GetGroup(id)
269268
if err != nil {
@@ -277,7 +276,7 @@ func TestSetGroupScene(t *testing.T) {
277276
}
278277

279278
func TestSetGroupTransitionTime(t *testing.T) {
280-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
279+
b := huego.New(hostname, username)
281280
id := 1
282281
group, err := b.GetGroup(id)
283282
if err != nil {
@@ -291,7 +290,7 @@ func TestSetGroupTransitionTime(t *testing.T) {
291290
}
292291

293292
func TestSetGroupEffect(t *testing.T) {
294-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
293+
b := huego.New(hostname, username)
295294
id := 1
296295
group, err := b.GetGroup(id)
297296
if err != nil {
@@ -305,7 +304,7 @@ func TestSetGroupEffect(t *testing.T) {
305304
}
306305

307306
func TestSetGroupAlert(t *testing.T) {
308-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
307+
b := huego.New(hostname, username)
309308
id := 1
310309
group, err := b.GetGroup(id)
311310
if err != nil {
@@ -319,7 +318,7 @@ func TestSetGroupAlert(t *testing.T) {
319318
}
320319

321320
func TestSetStateGroup(t *testing.T) {
322-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
321+
b := huego.New(hostname, username)
323322
id := 1
324323
group, err := b.GetGroup(id)
325324
if err != nil {
@@ -336,7 +335,7 @@ func TestSetStateGroup(t *testing.T) {
336335
}
337336

338337
func TestDeleteGroup(t *testing.T) {
339-
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
338+
b := huego.New(hostname, username)
340339
id := 3
341340
err := b.DeleteGroup(id)
342341
if err != nil {

huego_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
package huego_test
22

3-
// I'm too lazy to have this elsewhere
4-
// export HUE_USERNAME=9D6iHMbM-Bt7Kd0Cwh9Quo4tE02FMnmrNrnFAdAq
5-
// export HUE_HOSTNAME=192.168.1.59
6-
73
import (
84
"github.com/amimof/huego"
5+
"os"
96
"testing"
107
)
118

9+
// I'm too lazy to have this elsewhere
10+
// export HUE_USERNAME=9D6iHMbM-Bt7Kd0Cwh9Quo4tE02FMnmrNrnFAdAq
11+
// export HUE_HOSTNAME=192.168.1.59
12+
13+
var username string
14+
var hostname string
15+
16+
func init() {
17+
hostname = os.Getenv("HUE_HOSTNAME")
18+
username = os.Getenv("HUE_USERNAME")
19+
}
20+
1221
func TestDiscoverAndLoginLazy(t *testing.T) {
1322
b, _ := huego.Discover()
14-
b = b.Login("n7yx6YCUvV6-CGJZ5-VuyZoc3qgi9S2WjtEeDFpO")
23+
b = b.Login(username)
1524
t.Logf("Successfully logged in to bridge")
1625
}
1726

@@ -20,7 +29,7 @@ func TestDiscoverAndLogin(t *testing.T) {
2029
if err != nil {
2130
t.Fatal(err)
2231
}
23-
bridge = bridge.Login("n7yx6YCUvV6-CGJZ5-VuyZoc3qgi9S2WjtEeDFpO")
32+
bridge = bridge.Login(username)
2433
t.Logf("Successfully logged in to bridge")
2534
config, err := bridge.GetConfig()
2635
if err != nil {

0 commit comments

Comments
 (0)