diff --git a/.travis.yml b/.travis.yml index 3edf6b0..ca18f10 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ -sudo: false +os: linux dist: xenial language: go go: diff --git a/bridge_test.go b/bridge_test.go index 529104e..6b8af9f 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -1,14 +1,15 @@ -package huego_test +package huego import ( + "context" "fmt" - "github.com/amimof/huego" + "github.com/jarcoal/httpmock" "strings" "testing" ) func ExampleBridge_CreateUser() { - bridge, _ := huego.Discover() + bridge, _ := Discover() user, err := bridge.CreateUser("my awesome hue app") // Link button needs to be pressed if err != nil { fmt.Printf("Error creating user: %s", err.Error()) @@ -19,7 +20,7 @@ func ExampleBridge_CreateUser() { } func TestLogin(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) c, err := b.GetConfig() if err != nil { t.Fatal(err) @@ -29,7 +30,7 @@ func TestLogin(t *testing.T) { } func TestLoginUnauthorized(t *testing.T) { - b := huego.New(hostname, "") + b := New(hostname, "") b = b.Login("invalid_password") _, err := b.GetLights() if err != nil { @@ -42,7 +43,7 @@ func TestLoginUnauthorized(t *testing.T) { } func TestUpdateBridgeConfig(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) c, err := b.GetConfig() if err != nil { t.Fatal(err) @@ -52,3 +53,60 @@ func TestUpdateBridgeConfig(t *testing.T) { t.Fatal(err) } } + +func TestUpdateBridgeConfigError(t *testing.T) { + b := New(badHostname, username) + _, err := b.GetConfig() + if err == nil { + t.Fatal("Expected error not to be nil") + } +} + +func TestBridge_getAPIPathError(t *testing.T) { + b := New("invalid hostname", "") + expected := "parse http://invalid hostname: invalid character \" \" in host name" + _, err := b.getAPIPath("/") + if err.Error() != expected { + t.Fatalf("Expected error %s but got %s", expected, err.Error()) + } +} + +func TestBridge_getError(t *testing.T) { + httpmock.Deactivate() + defer httpmock.Activate() + expected := "Get invalid%20hostname: unsupported protocol scheme \"\"" + _, err := get(context.Background(), "invalid hostname") + if err.Error() != expected { + t.Fatalf("Expected error %s but got %s", expected, err.Error()) + } +} + +func TestBridge_putError(t *testing.T) { + httpmock.Deactivate() + defer httpmock.Activate() + expected := "Put invalid%20hostname: unsupported protocol scheme \"\"" + _, err := put(context.Background(), "invalid hostname", []byte("huego")) + if err.Error() != expected { + t.Fatalf("Expected error %s but got %s", expected, err.Error()) + } +} + +func TestBridge_postError(t *testing.T) { + httpmock.Deactivate() + defer httpmock.Activate() + expected := "Post invalid%20hostname: unsupported protocol scheme \"\"" + _, err := post(context.Background(), "invalid hostname", []byte("huego")) + if err.Error() != expected { + t.Fatalf("Expected error %s but got %s", expected, err.Error()) + } +} + +func TestBridge_deleteError(t *testing.T) { + httpmock.Deactivate() + defer httpmock.Activate() + expected := "Delete invalid%20hostname: unsupported protocol scheme \"\"" + _, err := delete(context.Background(), "invalid hostname") + if err.Error() != expected { + t.Fatalf("Expected error %s but got %s", expected, err.Error()) + } +} diff --git a/capabilities_test.go b/capabilities_test.go index f54a8d6..393bd19 100644 --- a/capabilities_test.go +++ b/capabilities_test.go @@ -1,13 +1,11 @@ -package huego_test +package huego import ( "testing" - - "github.com/amimof/huego" ) func TestGetCapabilities(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) c, err := b.GetCapabilities() if err != nil { t.Fatal(c) @@ -30,3 +28,11 @@ func TestGetCapabilities(t *testing.T) { t.Log(" Streaming") t.Logf(" Available: %d", c.Streaming.Available) } + +func TestGetCapabilitiesError(t *testing.T) { + b := New(badHostname, username) + _, err := b.GetCapabilities() + if err == nil { + t.Fatal("Expected error not to be nil") + } +} diff --git a/config_test.go b/config_test.go index b039bca..bac61d3 100644 --- a/config_test.go +++ b/config_test.go @@ -1,14 +1,12 @@ -package huego_test +package huego import ( - "testing" - - "github.com/amimof/huego" "github.com/stretchr/testify/assert" + "testing" ) func TestGetConfig(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) config, err := b.GetConfig() if err != nil { t.Fatal(err) @@ -65,8 +63,16 @@ func TestGetConfig(t *testing.T) { t.Logf("StarterKitID: %s", config.StarterKitID) } +func TestGetConfigError(t *testing.T) { + b := New(badHostname, username) + _, err := b.GetConfig() + if err == nil { + t.Fatal("Expected error not to be nil") + } +} + func TestCreateUser(t *testing.T) { - b := huego.New(hostname, "") + b := New(hostname, "") u, err := b.CreateUser("github.com/amimof/huego#go test") if err != nil { t.Fatal(err) @@ -75,8 +81,16 @@ func TestCreateUser(t *testing.T) { } } +func TestCreateUserError(t *testing.T) { + b := New(badHostname, username) + _, err := b.CreateUser("github.com/amimof/huego#go test") + if err == nil { + t.Fatal("Expected error not to be nil") + } +} + func TestGetUsers(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) users, err := b.GetUsers() if err != nil { t.Fatal(err) @@ -88,7 +102,7 @@ func TestGetUsers(t *testing.T) { t.Logf(" CreateDate: %s", u.CreateDate) t.Logf(" LastUseDate: %s", u.LastUseDate) } - contains := func(name string, ss []huego.Whitelist) bool { + contains := func(name string, ss []Whitelist) bool { for _, s := range ss { if s.Name == name { return true @@ -101,8 +115,16 @@ func TestGetUsers(t *testing.T) { assert.True(t, contains("MyApplication", users)) } +func TestGetUsersError(t *testing.T) { + b := New(badHostname, username) + _, err := b.GetUsers() + if err == nil { + t.Fatal("Expected error not to be nil") + } +} + func TestDeleteUser(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) err := b.DeleteUser("ffffffffe0341b1b376a2389376a2389") if err != nil { t.Fatal(err) @@ -111,9 +133,17 @@ func TestDeleteUser(t *testing.T) { } func TestGetFullState(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) _, err := b.GetFullState() if err != nil { t.Fatal(err) } } + +func TestGetFullStateError(t *testing.T) { + b := New(badHostname, username) + _, err := b.GetFullState() + if err == nil { + t.Fatal("Expected error not to be nil") + } +} diff --git a/go.mod b/go.mod index 709bb2a..f476c04 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/amimof/huego -go 1.12 +go 1.13 require ( github.com/jarcoal/httpmock v1.0.4 diff --git a/group_test.go b/group_test.go index c84eaa4..34a3fe0 100644 --- a/group_test.go +++ b/group_test.go @@ -1,14 +1,12 @@ -package huego_test +package huego import ( - "testing" - - "github.com/amimof/huego" "github.com/stretchr/testify/assert" + "testing" ) func TestGetGroups(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) groups, err := b.GetGroups() if err != nil { t.Fatal(err) @@ -44,7 +42,7 @@ func TestGetGroups(t *testing.T) { t.Logf(" ID: %d", g.ID) } - contains := func(name string, ss []huego.Group) bool { + contains := func(name string, ss []Group) bool { for _, s := range ss { if s.Name == name { return true @@ -55,10 +53,14 @@ func TestGetGroups(t *testing.T) { assert.True(t, contains("Group 1", groups)) assert.True(t, contains("Group 2", groups)) + + b.Host = badHostname + _, err = b.GetGroups() + assert.NotNil(t, err) } func TestGetGroup(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) g, err := b.GetGroup(1) if err != nil { t.Fatal(err) @@ -89,16 +91,21 @@ func TestGetGroup(t *testing.T) { t.Logf(" ColorMode: %s", g.State.ColorMode) t.Logf(" Reachable: %t", g.State.Reachable) t.Logf("ID: %d", g.ID) + + b.Host = badHostname + _, err = b.GetGroup(1) + assert.NotNil(t, err) } func TestCreateGroup(t *testing.T) { - b := huego.New(hostname, username) - resp, err := b.CreateGroup(huego.Group{ + b := New(hostname, username) + group := Group{ Name: "TestGroup", Type: "Room", Class: "Office", Lights: []string{}, - }) + } + resp, err := b.CreateGroup(group) if err != nil { t.Fatal(err) } else { @@ -107,15 +114,21 @@ func TestCreateGroup(t *testing.T) { t.Logf("%v: %s", k, v) } } + + b.Host = badHostname + _, err = b.CreateGroup(group) + assert.NotNil(t, err) + } func TestUpdateGroup(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 - resp, err := b.UpdateGroup(id, huego.Group{ + group := Group{ Name: "TestGroup (Updated)", Class: "Office", - }) + } + resp, err := b.UpdateGroup(id, group) if err != nil { t.Fatal(err) } else { @@ -124,16 +137,22 @@ func TestUpdateGroup(t *testing.T) { t.Logf("%v: %s", k, v) } } + + b.Host = badHostname + _, err = b.UpdateGroup(id, group) + assert.NotNil(t, err) + } func TestSetGroupState(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 - resp, err := b.SetGroupState(id, huego.State{ + state := State{ On: true, Bri: 150, Sat: 210, - }) + } + resp, err := b.SetGroupState(id, state) if err != nil { t.Fatal(err) } else { @@ -142,24 +161,34 @@ func TestSetGroupState(t *testing.T) { t.Logf("%v: %s", k, v) } } + + b.Host = badHostname + _, err = b.SetGroupState(id, state) + assert.NotNil(t, err) } func TestRenameGroup(t *testing.T) { - bridge := huego.New(hostname, username) + bridge := New(hostname, username) id := 1 group, err := bridge.GetGroup(id) if err != nil { t.Fatal(err) } - err = group.Rename("MyGroup (renamed)") + newName := "MyGroup (renamed)" + err = group.Rename(newName) if err != nil { t.Fatal(err) } t.Logf("Group renamed to %s", group.Name) + + bridge.Host = badHostname + err = group.Rename(newName) + assert.NotNil(t, err) + } func TestTurnOffGroup(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { @@ -171,10 +200,15 @@ func TestTurnOffGroup(t *testing.T) { } t.Logf("Turned off group with id %d", group.ID) t.Logf("Group IsOn: %t", group.State.On) + + b.Host = badHostname + err = group.Off() + assert.NotNil(t, err) + } func TestTurnOnGroup(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { @@ -186,10 +220,14 @@ func TestTurnOnGroup(t *testing.T) { } t.Logf("Turned on group with id %d", group.ID) t.Logf("Group IsOn: %t", group.State.On) + + b.Host = badHostname + err = group.On() + assert.NotNil(t, err) } func TestIfGroupIsOn(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { @@ -199,7 +237,7 @@ func TestIfGroupIsOn(t *testing.T) { } func TestSetGroupBri(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { @@ -210,10 +248,14 @@ func TestSetGroupBri(t *testing.T) { t.Fatal(err) } t.Logf("Brightness of group %d set to %d", group.ID, group.State.Bri) + + b.Host = badHostname + err = group.Bri(254) + assert.NotNil(t, err) } func TestSetGroupHue(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { @@ -224,10 +266,14 @@ func TestSetGroupHue(t *testing.T) { t.Fatal(err) } t.Logf("Hue of group %d set to %d", group.ID, group.State.Hue) + + b.Host = badHostname + err = group.Hue(65535) + assert.NotNil(t, err) } func TestSetGroupSat(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { @@ -238,24 +284,33 @@ func TestSetGroupSat(t *testing.T) { t.Fatal(err) } t.Logf("Sat of group %d set to %d", group.ID, group.State.Sat) + + b.Host = badHostname + err = group.Sat(254) + assert.NotNil(t, err) } func TestSetGroupXy(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { t.Fatal(err) } - err = group.Xy([]float32{0.1, 0.5}) + xy := []float32{0.1, 0.5} + err = group.Xy(xy) if err != nil { t.Fatal(err) } t.Logf("Xy of group %d set to %v", group.ID, group.State.Xy) + + b.Host = badHostname + err = group.Xy(xy) + assert.NotNil(t, err) } func TestSetGroupCt(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { @@ -266,24 +321,33 @@ func TestSetGroupCt(t *testing.T) { t.Fatal(err) } t.Logf("Ct of group %d set to %d", group.ID, group.State.Ct) + + b.Host = badHostname + err = group.Ct(16) + assert.NotNil(t, err) } func TestSetGroupScene(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { t.Fatal(err) } - err = group.Scene("2hgE1nGaITvy9VQ") + scene := "2hgE1nGaITvy9VQ" + err = group.Scene(scene) if err != nil { t.Fatal(err) } t.Logf("Scene of group %d set to %s", group.ID, group.State.Scene) + + b.Host = badHostname + err = group.Scene(scene) + assert.NotNil(t, err) } func TestSetGroupTransitionTime(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { @@ -294,10 +358,14 @@ func TestSetGroupTransitionTime(t *testing.T) { t.Fatal(err) } t.Logf("TransitionTime of group %d set to %d", group.ID, group.State.TransitionTime) + + b.Host = badHostname + err = group.TransitionTime(10) + assert.NotNil(t, err) } func TestSetGroupEffect(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { @@ -308,10 +376,14 @@ func TestSetGroupEffect(t *testing.T) { t.Fatal(err) } t.Logf("Effect of group %d set to %s", group.ID, group.State.Effect) + + b.Host = badHostname + err = group.Effect("colorloop") + assert.NotNil(t, err) } func TestSetGroupAlert(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { @@ -322,27 +394,36 @@ func TestSetGroupAlert(t *testing.T) { t.Fatal(err) } t.Logf("Alert of group %d set to %s", group.ID, group.State.Alert) + + b.Host = badHostname + err = group.Alert("lselect") + assert.NotNil(t, err) } func TestSetStateGroup(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 group, err := b.GetGroup(id) if err != nil { t.Fatal(err) } - err = group.SetState(huego.State{ + state := State{ On: true, Bri: 254, - }) + } + err = group.SetState(state) if err != nil { t.Fatal(err) } t.Logf("State set successfully on group %d", id) + + b.Host = badHostname + err = group.SetState(state) + assert.NotNil(t, err) } func TestDeleteGroup(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 err := b.DeleteGroup(id) if err != nil { diff --git a/huego_test.go b/huego_test.go index b7f438b..953a5e0 100644 --- a/huego_test.go +++ b/huego_test.go @@ -1,13 +1,12 @@ -package huego_test +package huego import ( "fmt" + "github.com/jarcoal/httpmock" + "github.com/stretchr/testify/assert" "os" "path" "testing" - - "github.com/amimof/huego" - "github.com/jarcoal/httpmock" ) // I'm too lazy to have this elsewhere @@ -16,6 +15,7 @@ import ( var username string var hostname string +var badHostname = "bad-hue-config" func init() { @@ -313,10 +313,73 @@ func init() { httpmock.RegisterResponder(test.method, test.url, httpmock.NewStringResponder(200, test.data)) } + // Register a responder for bad requests + paths := []string{ + "", + "config", + "/config", + "/config", + "/config/whitelist/ffffffffe0341b1b376a2389376a2389", + path.Join("/invalid_password", "/lights"), + path.Join(username, "/lights"), + path.Join(username, "/lights/1"), + path.Join(username, "/lights/new"), + path.Join(username, "/lights"), + path.Join(username, "/lights/1/state"), + path.Join(username, "/lights/1"), + path.Join(username, "/lights/1"), + "/groups", + "/groups/1", + "/groups/1", + "/groups/1/action", + "/groups", + "/groups/1", + "/scenes", + "/scenes/4e1c6b20e-on-0", + "/scenes", + "/scenes/4e1c6b20e-on-0", + "/scenes/4e1c6b20e-on-0/lightstates/1", + "/scenes/4e1c6b20e-on-0", + "/rules", + "/rules/1", + "/rules", + "/rules/1", + "/rules/1", + "/schedules", + "/schedules/1", + "/schedules", + "/schedules/1", + "/schedules/1", + "/sensors", + "/sensors/1", + "/sensors", + "/sensors", + "/sensors/new", + "/sensors/1", + "/sensors/1", + "/sensors/1/config", + "/sensors/1/state", + "/capabilities", + "/resourcelinks", + "/resourcelinks/1", + "/resourcelinks", + "/resourcelinks/1", + "/resourcelinks/1", + } + + // Register responder for errors + for _, p := range paths { + response := []byte("not json") + httpmock.RegisterResponder("GET", fmt.Sprintf("http://%s/api%s", badHostname, p), httpmock.NewBytesResponder(200, response)) + httpmock.RegisterResponder("POST", fmt.Sprintf("http://%s/api%s", badHostname, p), httpmock.NewBytesResponder(200, response)) + httpmock.RegisterResponder("PUT", fmt.Sprintf("http://%s/api%s", badHostname, p), httpmock.NewBytesResponder(200, response)) + httpmock.RegisterResponder("DELETE", fmt.Sprintf("http://%s/api%s", badHostname, p), httpmock.NewBytesResponder(200, response)) + } + } func TestDiscoverAndLogin(t *testing.T) { - bridge, err := huego.Discover() + bridge, err := Discover() if err != nil { t.Fatal(err) } @@ -325,7 +388,7 @@ func TestDiscoverAndLogin(t *testing.T) { } func TestDiscoverAllBridges(t *testing.T) { - bridges, err := huego.DiscoverAll() + bridges, err := DiscoverAll() if err != nil { t.Fatal(err) } @@ -337,3 +400,13 @@ func TestDiscoverAllBridges(t *testing.T) { t.Logf(" ID: %s", bridge.ID) } } + +func Test_unmarshalError(t *testing.T) { + s := struct { + Name string `json:"name"` + }{ + Name: "amimof", + } + err := unmarshal([]byte(`not json`), s) + assert.NotNil(t, err) +} diff --git a/light_test.go b/light_test.go index 63197ad..dbe9379 100644 --- a/light_test.go +++ b/light_test.go @@ -1,14 +1,12 @@ -package huego_test +package huego import ( - "testing" - - "github.com/amimof/huego" "github.com/stretchr/testify/assert" + "testing" ) func TestGetLights(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) lights, err := b.GetLights() if err != nil { t.Fatal(err) @@ -25,7 +23,7 @@ func TestGetLights(t *testing.T) { t.Logf(" SwConfigID: %s", l.SwConfigID) t.Logf(" ProductID: %s", l.ProductID) } - contains := func(name string, ss []huego.Light) bool { + contains := func(name string, ss []Light) bool { for _, s := range ss { if s.Name == name { return true @@ -36,10 +34,14 @@ func TestGetLights(t *testing.T) { assert.True(t, contains("Huecolorlamp7", lights)) assert.True(t, contains("Huelightstripplus1", lights)) + + b.Host = badHostname + _, err = b.GetLights() + assert.NotNil(t, err) } func TestGetLight(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) l, err := b.GetLight(1) if err != nil { t.Fatal(err) @@ -71,15 +73,21 @@ func TestGetLight(t *testing.T) { t.Logf(" ColorMode: %s", l.State.ColorMode) t.Logf(" Reachable: %t", l.State.Reachable) } + + b.Host = badHostname + _, err = b.GetLight(1) + assert.NotNil(t, err) + } func TestSetLight(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 - resp, err := b.SetLightState(id, huego.State{ + state := State{ On: true, Bri: 254, - }) + } + resp, err := b.SetLightState(id, state) if err != nil { t.Fatal(err) } else { @@ -88,10 +96,14 @@ func TestSetLight(t *testing.T) { t.Logf("%v: %s", k, v) } } + + b.Host = badHostname + _, err = b.SetLightState(id, state) + assert.NotNil(t, err) } func TestFindLights(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) resp, err := b.FindLights() if err != nil { t.Fatal(err) @@ -101,28 +113,18 @@ func TestFindLights(t *testing.T) { } } -} - -func TestGetNewLights(t *testing.T) { - b := huego.New(hostname, username) - newlights, err := b.GetNewLights() - if err != nil { - t.Fatal(err) - } - t.Logf("Found %d new lights", len(newlights.Lights)) - t.Logf("LastScan: %s", newlights.LastScan) - for _, light := range newlights.Lights { - t.Log(light) - } - + b.Host = badHostname + _, err = b.FindLights() + assert.NotNil(t, err) } func TestUpdateLight(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 - resp, err := b.UpdateLight(id, huego.Light{ + light := Light{ Name: "New Light", - }) + } + resp, err := b.UpdateLight(id, light) if err != nil { t.Fatal(err) } else { @@ -131,21 +133,13 @@ func TestUpdateLight(t *testing.T) { t.Logf("%v: %s", k, v) } } -} - -func TestDeleteLight(t *testing.T) { - b := huego.New(hostname, username) - id := 1 - err := b.DeleteLight(id) - if err != nil { - t.Fatal(err) - } else { - t.Logf("Light %d deleted", id) - } + b.Host = badHostname + _, err = b.UpdateLight(id, light) + assert.NotNil(t, err) } func TestTurnOffLight(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { @@ -156,10 +150,14 @@ func TestTurnOffLight(t *testing.T) { t.Fatal(err) } t.Logf("Turned off light with id %d", light.ID) + + b.Host = badHostname + err = light.Off() + assert.NotNil(t, err) } func TestTurnOnLight(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { @@ -170,10 +168,14 @@ func TestTurnOnLight(t *testing.T) { t.Fatal(err) } t.Logf("Turned on light with id %d", light.ID) + + b.Host = badHostname + err = light.On() + assert.NotNil(t, err) } func TestIfLightIsOn(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { @@ -183,21 +185,26 @@ func TestIfLightIsOn(t *testing.T) { } func TestRenameLight(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { t.Fatal(err) } - err = light.Rename("Color Lamp 1") + name := "Color Lamp 1" + err = light.Rename(name) if err != nil { t.Fatal(err) } t.Logf("Renamed light to '%s'", light.Name) + + b.Host = badHostname + err = light.Rename(name) + assert.NotNil(t, err) } func TestSetLightBri(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { @@ -208,10 +215,14 @@ func TestSetLightBri(t *testing.T) { t.Fatal(err) } t.Logf("Brightness of light %d set to %d", light.ID, light.State.Bri) + + b.Host = badHostname + err = light.Bri(254) + assert.NotNil(t, err) } func TestSetLightHue(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { @@ -222,10 +233,14 @@ func TestSetLightHue(t *testing.T) { t.Fatal(err) } t.Logf("Hue of light %d set to %d", light.ID, light.State.Hue) + + b.Host = badHostname + err = light.Hue(65535) + assert.NotNil(t, err) } func TestSetLightSat(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { @@ -236,24 +251,33 @@ func TestSetLightSat(t *testing.T) { t.Fatal(err) } t.Logf("Sat of light %d set to %d", light.ID, light.State.Sat) + + b.Host = badHostname + err = light.Sat(254) + assert.NotNil(t, err) } func TestSetLightXy(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { t.Fatal(err) } - err = light.Xy([]float32{0.1, 0.5}) + xy := []float32{0.1, 0.5} + err = light.Xy(xy) if err != nil { t.Fatal(err) } t.Logf("Xy of light %d set to %+v", light.ID, light.State.Xy) + + b.Host = badHostname + err = light.Xy(xy) + assert.NotNil(t, err) } func TestSetLightCt(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { @@ -264,10 +288,14 @@ func TestSetLightCt(t *testing.T) { t.Fatal(err) } t.Logf("Ct of light %d set to %d", light.ID, light.State.Ct) + + b.Host = badHostname + err = light.Ct(16) + assert.NotNil(t, err) } func TestSetLightTransitionTime(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { @@ -278,10 +306,14 @@ func TestSetLightTransitionTime(t *testing.T) { t.Fatal(err) } t.Logf("TransitionTime of light %d set to %d", light.ID, light.State.TransitionTime) + + b.Host = badHostname + err = light.TransitionTime(10) + assert.NotNil(t, err) } func TestSetLightEffect(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { @@ -292,10 +324,14 @@ func TestSetLightEffect(t *testing.T) { t.Fatal(err) } t.Logf("Effect of light %d set to %s", light.ID, light.State.Effect) + + b.Host = badHostname + err = light.Effect("colorloop") + assert.NotNil(t, err) } func TestSetLightAlert(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { @@ -306,21 +342,30 @@ func TestSetLightAlert(t *testing.T) { t.Fatal(err) } t.Logf("Alert of light %d set to %s", light.ID, light.State.Alert) + + b.Host = badHostname + err = light.Alert("lselect") + assert.NotNil(t, err) } func TestSetStateLight(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 light, err := b.GetLight(id) if err != nil { t.Fatal(err) } - err = light.SetState(huego.State{ + state := State{ On: true, Bri: 254, - }) + } + err = light.SetState(state) if err != nil { t.Fatal(err) } t.Logf("State set successfully on light %d", id) + + b.Host = badHostname + err = light.SetState(state) + assert.NotNil(t, err) } diff --git a/resourcelink_test.go b/resourcelink_test.go index e95df18..0768b91 100644 --- a/resourcelink_test.go +++ b/resourcelink_test.go @@ -1,14 +1,12 @@ -package huego_test +package huego import ( - "testing" - - "github.com/amimof/huego" "github.com/stretchr/testify/assert" + "testing" ) func TestGetResourcelinks(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) resourcelinks, err := b.GetResourcelinks() if err != nil { t.Fatal(err) @@ -25,7 +23,7 @@ func TestGetResourcelinks(t *testing.T) { t.Logf(" ID: %d", resourcelink.ID) } - contains := func(name string, ss []*huego.Resourcelink) bool { + contains := func(name string, ss []*Resourcelink) bool { for _, s := range ss { if s.Name == name { return true @@ -36,10 +34,14 @@ func TestGetResourcelinks(t *testing.T) { assert.True(t, contains("Sunrise", resourcelinks)) assert.True(t, contains("Sunrise 2", resourcelinks)) + + b.Host = badHostname + _, err = b.GetResourcelinks() + assert.NotNil(t, err) } func TestGetResourcelink(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) l, err := b.GetResourcelink(1) if err != nil { t.Fatal(err) @@ -51,11 +53,15 @@ func TestGetResourcelink(t *testing.T) { t.Logf("Owner: %s", l.Owner) t.Logf("Links: %s", l.Links) t.Logf("ID: %d", l.ID) + + b.Host = badHostname + _, err = b.GetResourcelink(1) + assert.NotNil(t, err) } func TestCreateResourcelink(t *testing.T) { - b := huego.New(hostname, username) - resourcelink := &huego.Resourcelink{ + b := New(hostname, username) + resourcelink := &Resourcelink{ Name: "Huego Test Resourcelink", Description: "Amir's wakeup experience", Type: "Link", @@ -72,15 +78,19 @@ func TestCreateResourcelink(t *testing.T) { t.Logf("%v: %s", k, v) } + b.Host = badHostname + _, err = b.CreateResourcelink(resourcelink) + assert.NotNil(t, err) } func TestUpdateResourcelink(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 - resp, err := b.UpdateResourcelink(id, &huego.Resourcelink{ + rl := &Resourcelink{ Name: "New Resourcelink", Description: "Updated Attribute", - }) + } + resp, err := b.UpdateResourcelink(id, rl) if err != nil { t.Fatal(err) } @@ -89,10 +99,13 @@ func TestUpdateResourcelink(t *testing.T) { t.Logf("%v: %s", k, v) } + b.Host = badHostname + _, err = b.UpdateResourcelink(id, rl) + assert.NotNil(t, err) } func TestDeleteResourcelink(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 err := b.DeleteResourcelink(1) if err != nil { diff --git a/rule_test.go b/rule_test.go index 6afac52..7223c68 100644 --- a/rule_test.go +++ b/rule_test.go @@ -1,14 +1,12 @@ -package huego_test +package huego import ( - "testing" - - "github.com/amimof/huego" "github.com/stretchr/testify/assert" + "testing" ) func TestGetRules(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) rules, err := b.GetRules() if err != nil { t.Fatal(err) @@ -18,7 +16,7 @@ func TestGetRules(t *testing.T) { t.Log(rule) } - contains := func(name string, ss []*huego.Rule) bool { + contains := func(name string, ss []*Rule) bool { for _, s := range ss { if s.Name == name { return true @@ -29,35 +27,43 @@ func TestGetRules(t *testing.T) { assert.True(t, contains("Wall Switch Rule", rules)) assert.True(t, contains("Wall Switch Rule 2", rules)) + + b.Host = badHostname + _, err = b.GetRules() + assert.NotNil(t, err) } func TestGetRule(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) l, err := b.GetRule(1) if err != nil { t.Fatal(err) } else { t.Log(l) } + + b.Host = badHostname + _, err = b.GetRule(1) + assert.NotNil(t, err) } func TestCreateRule(t *testing.T) { - b := huego.New(hostname, username) - conditions := []*huego.Condition{ + b := New(hostname, username) + conditions := []*Condition{ { Address: "/sensors/2/state/buttonevent", Operator: "eq", Value: "16", }, } - actions := []*huego.RuleAction{ + actions := []*RuleAction{ { Address: "/groups/0/action", Method: "PUT", - Body: &huego.State{On: true}, + Body: &State{On: true}, }, } - rule := &huego.Rule{ + rule := &Rule{ Name: "Huego Test Rule", Conditions: conditions, Actions: actions, @@ -71,20 +77,25 @@ func TestCreateRule(t *testing.T) { t.Logf("%v: %s", k, v) } } + + b.Host = badHostname + _, err = b.CreateRule(rule) + assert.NotNil(t, err) } func TestUpdateRule(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 - resp, err := b.UpdateRule(id, &huego.Rule{ - Actions: []*huego.RuleAction{ + rule := &Rule{ + Actions: []*RuleAction{ { Address: "/groups/1/action", Method: "PUT", - Body: &huego.State{On: true}, + Body: &State{On: true}, }, }, - }) + } + resp, err := b.UpdateRule(id, rule) if err != nil { t.Fatal(err) } else { @@ -93,10 +104,13 @@ func TestUpdateRule(t *testing.T) { t.Logf("%v: %s", k, v) } } + b.Host = badHostname + _, err = b.UpdateRule(id, rule) + assert.NotNil(t, err) } func TestDeleteRule(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 err := b.DeleteRule(id) if err != nil { diff --git a/scene_test.go b/scene_test.go index d0e1213..07b7fd2 100644 --- a/scene_test.go +++ b/scene_test.go @@ -1,14 +1,12 @@ -package huego_test +package huego import ( - "testing" - - "github.com/amimof/huego" "github.com/stretchr/testify/assert" + "testing" ) func TestGetScenes(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) scenes, err := b.GetScenes() if err != nil { t.Fatal(err) @@ -31,7 +29,7 @@ func TestGetScenes(t *testing.T) { t.Logf(" ID: %s", scene.ID) } - contains := func(name string, ss []huego.Scene) bool { + contains := func(name string, ss []Scene) bool { for _, s := range ss { if s.Name == name { return true @@ -43,11 +41,16 @@ func TestGetScenes(t *testing.T) { assert.True(t, contains("Kathyon1449133269486", scenes)) assert.True(t, contains("Cozydinner", scenes)) + b.Host = badHostname + _, err = b.GetScenes() + assert.NotNil(t, err) + } func TestGetScene(t *testing.T) { - b := huego.New(hostname, username) - s, err := b.GetScene("4e1c6b20e-on-0") + b := New(hostname, username) + id := "4e1c6b20e-on-0" + s, err := b.GetScene(id) if err != nil { t.Fatal(err) } @@ -68,15 +71,20 @@ func TestGetScene(t *testing.T) { for k := range s.LightStates { t.Logf(" Light %d: %+v", k, s.LightStates[k]) } + + b.Host = badHostname + _, err = b.GetScene(id) + assert.NotNil(t, err) } func TestCreateScene(t *testing.T) { - b := huego.New(hostname, username) - resp, err := b.CreateScene(&huego.Scene{ + b := New(hostname, username) + scene := &Scene{ Name: "New Scene", Lights: []string{"4", "5"}, Recycle: true, - }) + } + resp, err := b.CreateScene(scene) if err != nil { t.Fatal(err) } @@ -84,18 +92,23 @@ func TestCreateScene(t *testing.T) { for k, v := range resp.Success { t.Logf("%v: %s", k, v) } + + b.Host = badHostname + _, err = b.CreateScene(scene) + assert.NotNil(t, err) } func TestUpdateScene(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) scene, err := b.GetScene("4e1c6b20e-on-0") if err != nil { t.Fatal(err) } - resp, err := b.UpdateScene(scene.ID, &huego.Scene{ + newscene := &Scene{ Name: "New Scene", Lights: []string{}, - }) + } + resp, err := b.UpdateScene(scene.ID, newscene) if err != nil { t.Fatal(err) } @@ -103,31 +116,40 @@ func TestUpdateScene(t *testing.T) { for k, v := range resp.Success { t.Logf("%v: %s", k, v) } + + b.Host = badHostname + _, err = b.UpdateScene(scene.ID, newscene) + assert.NotNil(t, err) } func TestSetSceneLightState(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) scene, err := b.GetScene("4e1c6b20e-on-0") if err != nil { t.Fatal(err) } light := 1 + state := &State{ + On: true, + Bri: 255, + } t.Logf("Name: %s", scene.Name) t.Logf("ID: %s", scene.ID) t.Logf("LightStates: %+v", scene.LightStates) - _, err = b.SetSceneLightState(scene.ID, light, &huego.State{ - On: true, - Bri: 255, - }) + _, err = b.SetSceneLightState(scene.ID, light, state) if err != nil { t.Fatal(err) } t.Logf("Successfully set the state of light %d of scene '%s'", light, scene.Name) + b.Host = badHostname + _, err = b.SetSceneLightState(scene.ID, light, state) + assert.NotNil(t, err) + } func TestDeleteScene(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) scene, err := b.GetScene("4e1c6b20e-on-0") if err != nil { t.Fatal(err) @@ -140,7 +162,7 @@ func TestDeleteScene(t *testing.T) { } func TestRecallScene(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) scene := "4e1c6b20e-on-0" group := 1 resp, err := b.RecallScene("HcK1mEcgS7gcVcT", group) @@ -151,10 +173,14 @@ func TestRecallScene(t *testing.T) { for k, v := range resp.Success { t.Logf("%v: %s", k, v) } + + b.Host = badHostname + _, err = b.RecallScene("HcK1mEcgS7gcVcT", group) + assert.NotNil(t, err) } func TestRecallScene2(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) group := 1 scene, err := b.GetScene("4e1c6b20e-on-0") if err != nil { @@ -165,4 +191,8 @@ func TestRecallScene2(t *testing.T) { t.Fatal(err) } t.Logf("Scene %s (%s) recalled in group %d", scene.Name, scene.ID, group) + + b.Host = badHostname + err = scene.Recall(group) + assert.NotNil(t, err) } diff --git a/schedule_test.go b/schedule_test.go index dc8b4fe..c53e484 100644 --- a/schedule_test.go +++ b/schedule_test.go @@ -1,14 +1,12 @@ -package huego_test +package huego import ( - "testing" - - "github.com/amimof/huego" "github.com/stretchr/testify/assert" + "testing" ) func TestGetSchedules(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) schedules, err := b.GetSchedules() if err != nil { t.Fatal(err) @@ -30,7 +28,7 @@ func TestGetSchedules(t *testing.T) { t.Logf(" ID: %d", schedule.ID) } - contains := func(name string, ss []*huego.Schedule) bool { + contains := func(name string, ss []*Schedule) bool { for _, s := range ss { if s.Name == name { return true @@ -42,10 +40,13 @@ func TestGetSchedules(t *testing.T) { assert.True(t, contains("Timer", schedules)) assert.True(t, contains("Alarm", schedules)) + b.Host = badHostname + _, err = b.GetSchedules() + assert.NotNil(t, err) } func TestGetSchedule(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) s, err := b.GetSchedule(1) if err != nil { t.Fatal(err) @@ -56,11 +57,15 @@ func TestGetSchedule(t *testing.T) { t.Logf("Status: %s", s.Status) t.Logf("AutoDelete: %t", s.AutoDelete) t.Logf("ID: %d", s.ID) + + b.Host = badHostname + _, err = b.GetSchedule(1) + assert.NotNil(t, err) } func TestCreateSchedule(t *testing.T) { - b := huego.New(hostname, username) - command := &huego.Command{ + b := New(hostname, username) + command := &Command{ Address: "/api/" + username + "/lights/0", Body: &struct { on bool @@ -69,7 +74,7 @@ func TestCreateSchedule(t *testing.T) { }, Method: "PUT", } - schedule := &huego.Schedule{ + schedule := &Schedule{ Name: "TestSchedule", Description: "Huego test schedule", Command: command, @@ -84,15 +89,20 @@ func TestCreateSchedule(t *testing.T) { t.Logf("%v: %s", k, v) } } + + b.Host = badHostname + _, err = b.CreateSchedule(schedule) + assert.NotNil(t, err) } func TestUpdateSchedule(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 - resp, err := b.UpdateSchedule(id, &huego.Schedule{ + schedule := &Schedule{ Name: "New Scehdule", Description: "Updated parameter", - }) + } + resp, err := b.UpdateSchedule(id, schedule) if err != nil { t.Fatal(err) } else { @@ -101,10 +111,14 @@ func TestUpdateSchedule(t *testing.T) { t.Logf("%v: %s", k, v) } } + + b.Host = badHostname + _, err = b.UpdateSchedule(id, schedule) + assert.NotNil(t, err) } func TestDeleteSchedule(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 err := b.DeleteSchedule(id) if err != nil { diff --git a/sensor_test.go b/sensor_test.go index 6ae5731..eeb5ccb 100644 --- a/sensor_test.go +++ b/sensor_test.go @@ -1,14 +1,12 @@ -package huego_test +package huego import ( - "testing" - - "github.com/amimof/huego" "github.com/stretchr/testify/assert" + "testing" ) func TestGetSensors(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) sensors, err := b.GetSensors() if err != nil { t.Fatal(err) @@ -27,7 +25,7 @@ func TestGetSensors(t *testing.T) { t.Logf("SwVersion: %s", sensor.SwVersion) t.Logf("ID: %d", sensor.ID) } - contains := func(name string, ss []huego.Sensor) bool { + contains := func(name string, ss []Sensor) bool { for _, s := range ss { if s.Name == name { return true @@ -38,10 +36,14 @@ func TestGetSensors(t *testing.T) { assert.True(t, contains("Daylight", sensors)) assert.True(t, contains("Tap Switch 2", sensors)) + + b.Host = badHostname + _, err = b.GetSensors() + assert.NotNil(t, err) } func TestGetSensor(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) sensor, err := b.GetSensor(1) if err != nil { t.Fatal(err) @@ -57,13 +59,18 @@ func TestGetSensor(t *testing.T) { t.Logf("UniqueID: %s", sensor.UniqueID) t.Logf("SwVersion: %s", sensor.SwVersion) t.Logf("ID: %d", sensor.ID) + + b.Host = badHostname + _, err = b.GetSensor(1) + assert.NotNil(t, err) } func TestCreateSensor(t *testing.T) { - b := huego.New(hostname, username) - resp, err := b.CreateSensor(&huego.Sensor{ + b := New(hostname, username) + sensor := &Sensor{ Name: "New Sensor", - }) + } + resp, err := b.CreateSensor(sensor) if err != nil { t.Fatal(err) } @@ -72,10 +79,13 @@ func TestCreateSensor(t *testing.T) { t.Logf("%v: %s", k, v) } + b.Host = badHostname + _, err = b.CreateSensor(sensor) + assert.NotNil(t, err) } func TestFindSensors(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) resp, err := b.FindSensors() if err != nil { t.Fatal(err) @@ -84,10 +94,13 @@ func TestFindSensors(t *testing.T) { t.Logf("%v: %s", k, v) } + b.Host = badHostname + _, err = b.FindSensors() + assert.NotNil(t, err) } func TestGetNewSensors(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) newSensors, err := b.GetNewSensors() if err != nil { t.Fatal(err) @@ -107,7 +120,7 @@ func TestGetNewSensors(t *testing.T) { t.Logf("ID: %d", sensor.ID) } - contains := func(name string, ss []*huego.Sensor) bool { + contains := func(name string, ss []*Sensor) bool { for _, s := range ss { if s.Name == name { return true @@ -121,21 +134,26 @@ func TestGetNewSensors(t *testing.T) { } func TestUpdateSensor(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 - resp, err := b.UpdateSensor(id, &huego.Sensor{ + sensor := &Sensor{ Name: "New Sensor", - }) + } + resp, err := b.UpdateSensor(id, sensor) if err != nil { t.Fatal(err) } for k, v := range resp.Success { t.Logf("%v: %s", k, v) } + + b.Host = badHostname + _, err = b.UpdateSensor(id, sensor) + assert.NotNil(t, err) } func TestUpdateSensorConfig(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 resp, err := b.UpdateSensorConfig(id, "test") if err != nil { @@ -145,10 +163,13 @@ func TestUpdateSensorConfig(t *testing.T) { t.Logf("%v: %s", k, v) } + b.Host = badHostname + _, err = b.UpdateSensorConfig(id, "test") + assert.NotNil(t, err) } func TestDeleteSensor(t *testing.T) { - b := huego.New(hostname, username) + b := New(hostname, username) id := 1 err := b.DeleteSensor(id) if err != nil {