github.com/amimof/huego@v1.2.1/config_test.go (about)

     1  package huego
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestGetConfig(t *testing.T) {
    10  	b := New(hostname, username)
    11  	config, err := b.GetConfig()
    12  	if err != nil {
    13  		t.Fatal(err)
    14  	}
    15  	t.Logf("Name: %s", config.Name)
    16  	t.Logf("SwUpdate:")
    17  	t.Logf("  CheckForUpdate: %t", config.SwUpdate.CheckForUpdate)
    18  	t.Logf("  DeviceTypes:")
    19  	t.Logf("    Bridge: %t", config.SwUpdate.DeviceTypes.Bridge)
    20  	t.Logf("    Lights (length): %d", len(config.SwUpdate.DeviceTypes.Lights))
    21  	t.Logf("    Sensors (length): %d", len(config.SwUpdate.DeviceTypes.Sensors))
    22  	t.Logf("  UpdateState: %d", config.SwUpdate.UpdateState)
    23  	t.Logf("  Notify: %t", config.SwUpdate.Notify)
    24  	t.Logf("  URL: %s", config.SwUpdate.URL)
    25  	t.Logf("  Text: %s", config.SwUpdate.Text)
    26  	t.Logf("SwUpdate2:")
    27  	t.Logf("  Bridge: %s", config.SwUpdate2.Bridge)
    28  	t.Logf("    State: %s", config.SwUpdate2.Bridge.State)
    29  	t.Logf("    LastInstall: %s", config.SwUpdate2.Bridge.LastInstall)
    30  	t.Logf("  CheckForUpdate: %t", config.SwUpdate2.CheckForUpdate)
    31  	t.Logf("  State: %s", config.SwUpdate2.State)
    32  	t.Logf("  Install: %t", config.SwUpdate2.Install)
    33  	t.Logf("  AutoInstall:")
    34  	t.Logf("    On: %t", config.SwUpdate2.AutoInstall.On)
    35  	t.Logf("    UpdateTime: %s", config.SwUpdate2.AutoInstall.UpdateTime)
    36  	t.Logf("  LastChange: %s", config.SwUpdate2.LastChange)
    37  	t.Logf("  LastInstall: %s", config.SwUpdate2.LastInstall)
    38  	t.Logf("Whitelist (length): %d", len(config.Whitelist))
    39  	t.Logf("PortalState:")
    40  	t.Logf("  SignedOn: %t", config.PortalState.SignedOn)
    41  	t.Logf("  Incoming: %t", config.PortalState.Incoming)
    42  	t.Logf("  Outgoing: %t", config.PortalState.Outgoing)
    43  	t.Logf("  Communication: %s", config.PortalState.Communication)
    44  	t.Logf("APIVersion: %s", config.APIVersion)
    45  	t.Logf("SwVersion: %s", config.SwVersion)
    46  	t.Logf("ProxyAddress: %s", config.ProxyAddress)
    47  	t.Logf("ProxyPort: %d", config.ProxyPort)
    48  	t.Logf("LinkButton: %t", config.LinkButton)
    49  	t.Logf("IPAddress: %s", config.IPAddress)
    50  	t.Logf("Mac: %s", config.Mac)
    51  	t.Logf("NetMask: %s", config.NetMask)
    52  	t.Logf("Gateway: %s", config.Gateway)
    53  	t.Logf("Dhcp: %t", config.Dhcp)
    54  	t.Logf("PortalServices: %t", config.PortalServices)
    55  	t.Logf("UTC: %s", config.UTC)
    56  	t.Logf("LocalTime: %s", config.LocalTime)
    57  	t.Logf("TimeZone: %s", config.TimeZone)
    58  	t.Logf("ZigbeeChannel: %d", config.ZigbeeChannel)
    59  	t.Logf("ModelID: %s", config.ModelID)
    60  	t.Logf("BridgeID: %s", config.BridgeID)
    61  	t.Logf("FactoryNew: %t", config.FactoryNew)
    62  	t.Logf("ReplacesBridgeID: %s", config.ReplacesBridgeID)
    63  	t.Logf("DatastoreVersion: %s", config.DatastoreVersion)
    64  	t.Logf("StarterKitID: %s", config.StarterKitID)
    65  }
    66  
    67  func TestGetConfigError(t *testing.T) {
    68  	b := New(badHostname, username)
    69  	_, err := b.GetConfig()
    70  	if err == nil {
    71  		t.Fatal("Expected error not to be nil")
    72  	}
    73  }
    74  
    75  func TestCreateUser(t *testing.T) {
    76  	b := New(hostname, "")
    77  	u, err := b.CreateUser("github.com/amimof/huego#go test")
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	} else {
    81  		t.Logf("User created with username: %s", u)
    82  	}
    83  }
    84  
    85  func TestCreateUserError(t *testing.T) {
    86  	b := New(badHostname, username)
    87  	_, err := b.CreateUser("github.com/amimof/huego#go test")
    88  	if err == nil {
    89  		t.Fatal("Expected error not to be nil")
    90  	}
    91  }
    92  
    93  func TestCreateUserWithClientKey(t *testing.T) {
    94  	b := New(hostname, "")
    95  	u, err := b.CreateUserWithClientKey("github.com/amimof/huego#go test")
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	} else {
    99  		t.Logf("User created with username: %s", u)
   100  	}
   101  }
   102  
   103  func TestCreateUserWithClientKeyError(t *testing.T) {
   104  	b := New(badHostname, username)
   105  	_, err := b.CreateUserWithClientKey("github.com/amimof/huego#go test")
   106  	if err == nil {
   107  		t.Fatal("Expected error not to be nil")
   108  	}
   109  }
   110  
   111  func TestGetUsers(t *testing.T) {
   112  	b := New(hostname, username)
   113  	users, err := b.GetUsers()
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	for i, u := range users {
   118  		t.Logf("%d:", i)
   119  		t.Logf("  Name: %s", u.Name)
   120  		t.Logf("  Username: %s", u.Username)
   121  		t.Logf("  CreateDate: %s", u.CreateDate)
   122  		t.Logf("  LastUseDate: %s", u.LastUseDate)
   123  	}
   124  	contains := func(name string, ss []Whitelist) bool {
   125  		for _, s := range ss {
   126  			if s.Name == name {
   127  				return true
   128  			}
   129  		}
   130  		return false
   131  	}
   132  
   133  	assert.True(t, contains("PhilipsHueAndroidApp#TCTALCATELONETOU", users))
   134  	assert.True(t, contains("MyApplication", users))
   135  }
   136  
   137  func TestGetUsersError(t *testing.T) {
   138  	b := New(badHostname, username)
   139  	_, err := b.GetUsers()
   140  	if err == nil {
   141  		t.Fatal("Expected error not to be nil")
   142  	}
   143  }
   144  
   145  func TestDeleteUser(t *testing.T) {
   146  	b := New(hostname, username)
   147  	err := b.DeleteUser("ffffffffe0341b1b376a2389376a2389")
   148  	if err != nil {
   149  		t.Fatal(err)
   150  	}
   151  	t.Logf("Deleted user '%s'", "ffffffffe0341b1b376a2389376a2389")
   152  }
   153  
   154  func TestGetFullState(t *testing.T) {
   155  	b := New(hostname, username)
   156  	_, err := b.GetFullState()
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  }
   161  
   162  func TestGetFullStateError(t *testing.T) {
   163  	b := New(badHostname, username)
   164  	_, err := b.GetFullState()
   165  	if err == nil {
   166  		t.Fatal("Expected error not to be nil")
   167  	}
   168  }