github.com/tommi2day/gomodules@v1.13.2-0.20240423190010-b7d55d252a27/hmlib/device_test.go (about)

     1  package hmlib
     2  
     3  import (
     4  	"net/url"
     5  	"testing"
     6  
     7  	"github.com/jarcoal/httpmock"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"github.com/tommi2day/gomodules/test"
    11  )
    12  
    13  func TestDevice(t *testing.T) {
    14  	var err error
    15  	test.Testinit(t)
    16  	httpmock.ActivateNonDefault(httpClient.GetClient())
    17  	defer httpmock.DeactivateAndReset()
    18  	hmURL = MockURL
    19  	hmToken = MockToken
    20  	var deviceList DeviceListResponse
    21  	t.Run("device list", func(t *testing.T) {
    22  		response := DeviceListTest
    23  		responder := httpmock.NewStringResponder(200, response)
    24  		fakeURL := hmURL + DeviceListEndpoint
    25  		httpmock.RegisterResponder("GET", fakeURL, responder)
    26  
    27  		deviceList, err = GetDeviceList("", false)
    28  		assert.NoErrorf(t, err, "GetDeviceList should not return an error:%s", err)
    29  		require.Greater(t, len(deviceList.DeviceListEntries), 0, "GetDeviceList should return entries")
    30  		assert.Equal(t, 1, len(DeviceAddressMap), "GetDeviceList should return 1 entry")
    31  		assert.Equal(t, 1, len(DeviceIDMap), "GetDeviceList should return 1 entry")
    32  		d, ok := DeviceAddressMap["000955699D3D84"]
    33  		assert.True(t, ok, "GetDeviceList should contain 000955699D3D84")
    34  		assert.Equal(t, "Bewegungsmelder Garage", d.Name, "GetDeviceList should contain Bewegungsmelder Garage")
    35  		d, ok = DeviceIDMap["4740"]
    36  		assert.True(t, ok, "GetDeviceList should contain 4740")
    37  		assert.Equal(t, "Bewegungsmelder Garage", d.Name, "GetDeviceList should contain Bewegungsmelder Garage")
    38  		assert.Equal(t, "HmIP-SMO", d.Type, "GetDeviceList should contain HmIP-SMO")
    39  		assert.Equal(t, len(d.Channels), 4, "GetDeviceList should contain 4 channels")
    40  		t.Log(deviceList.String())
    41  	})
    42  	t.Run("device list empty", func(t *testing.T) {
    43  		response := DeviceListEmptyTest
    44  		responder := httpmock.NewStringResponder(200, response)
    45  		fakeURL := hmURL + DeviceListEndpoint
    46  		httpmock.RegisterResponder("GET", fakeURL, responder)
    47  		deviceList, err = GetDeviceList("", true)
    48  		assert.NoErrorf(t, err, "GetDeviceList should not return an error:%s", err)
    49  		require.Equal(t, len(deviceList.DeviceListEntries), 0, "GetDeviceList should return entries")
    50  		assert.Equal(t, 0, len(DeviceAddressMap), "GetDeviceList should return 0 entry")
    51  		assert.Equal(t, 0, len(DeviceIDMap), "GetDeviceList should return 0 entry")
    52  	})
    53  	t.Run("device types list", func(t *testing.T) {
    54  		response := DeviceTypeListTest
    55  		responder := httpmock.NewStringResponder(200, response)
    56  		fakeURL := hmURL + DeviceTypeListEndpoint
    57  		httpmock.RegisterResponder("GET", fakeURL, responder)
    58  		deviceTypes, err := GetDeviceTypeList()
    59  		assert.NoErrorf(t, err, "GetDeviceTypes should not return an error:%s", err)
    60  		assert.Equal(t, 5, len(deviceTypes.DeviceTypeListEntries), "GetDeviceTypes should return 5 entries")
    61  		if len(deviceTypes.DeviceTypeListEntries) > 0 {
    62  			e := deviceTypes.DeviceTypeListEntries[0]
    63  			assert.Equal(t, "HM-RC-Sec4-3", e.Name, "GetDeviceType[0] should return HM-RC-Sec4-3")
    64  			assert.Equal(t, "HM-RC-4", e.Description, "GetDeviceType[0] should return HM-RC-4")
    65  			l := len(e.Forms)
    66  			assert.Greater(t, l, 1, "GetDeviceType[0] should return more 1 form")
    67  			if l > 0 {
    68  				t.Logf("Form[0]:%s", e.Forms[0].String())
    69  			}
    70  		}
    71  		t.Log(deviceTypes.String())
    72  	})
    73  	t.Run("device types list not autenticated", func(t *testing.T) {
    74  		fakeURL := "http://localhost:80" + DeviceTypeListEndpoint
    75  		sid := "xxx"
    76  		SetHmToken(sid)
    77  		SetHmURL("http://localhost:80")
    78  		q := url.Values{
    79  			"sid": []string{sid},
    80  		}
    81  		httpmock.RegisterResponderWithQuery(
    82  			"GET", fakeURL, q,
    83  			httpmock.NewStringResponder(200, DeviceTypeListNotAuthTest))
    84  		deviceTypes, err := GetDeviceTypeList()
    85  		assert.Errorf(t, err, "GetDeviceTypes should return an error:%s", err)
    86  		assert.Equal(t, 0, len(deviceTypes.DeviceTypeListEntries), "GetDeviceTypes should return 0 entries")
    87  	})
    88  }