From ec54fd96a4bdab9fc3279ccbfd3bc1d992816acd Mon Sep 17 00:00:00 2001 From: awilliams <645192+awilliams@users.noreply.github.com> Date: Sat, 21 Oct 2023 21:18:30 -0600 Subject: [PATCH] Fix integration test / Docker Compose env The Homeassistant setup needs updating for newer version. This allows for a basic instance of HASS to be run via docker compose, and then for wifi-presence to send events to it via MQTT. --- test/Makefile | 44 +++++++++++++++++++-- test/create-ha-account.bash | 76 +++++++++++++++++++++++++++++++++++++ test/docker-compose.yml | 4 +- test/ha.yaml | 5 +-- 4 files changed, 120 insertions(+), 9 deletions(-) create mode 100755 test/create-ha-account.bash diff --git a/test/Makefile b/test/Makefile index 573b565..0f448e3 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,4 +1,42 @@ -.PHONY: +.PHONY: test test: - go test -race ../... - go test . ../internal/presence ../internal/hass -race -v -mqttAddr "tcp://localhost:1883" + go test \ + -race \ + ../... + go test \ + . \ + ../internal/presence \ + ../internal/hass \ + -race \ + -v \ + -mqttAddr "tcp://localhost:1883" + +.PHONY: wifi-presence +wifi-presence: + go run ../cmd/wifi-presence \ + -apName="test-AP" \ + -debounce="2s" \ + -hass.autodiscovery=true \ + -mqtt.addr="tcp://localhost:1883" \ + -hostapd.socks="./hostapd.sock" \ + -verbose + +.PHONY: hostap-ctrl +hostap-ctrl: + go run ./hostap-ctrl + +.PHONY: send-cfg +send-cfg: + echo '{"devices":[{"name":"test-phone","mac":"BE:EF:00:00:FA:CE"}]}' \ + | mosquitto_pub \ + -h 'localhost' \ + -t 'wifi-presence/config' \ + -l + +.PHONY: send-empty-cfg +send-empty-cfg: + echo '{"devices":[]}' \ + | mosquitto_pub \ + -h 'localhost' \ + -t 'wifi-presence/config' \ + -l diff --git a/test/create-ha-account.bash b/test/create-ha-account.bash new file mode 100755 index 0000000..e48b2fd --- /dev/null +++ b/test/create-ha-account.bash @@ -0,0 +1,76 @@ +#!/usr/bin/env bash +# +# This script configures a fresh instance of HASS running +# as part of the associated Docker Compose environment. + +set -e + +HAURL="http://localhost:8123" + +HANAME="admin" +HAUSER="${HANAME}" +HAPASSWORD="${HANAME}" + +# Create temporary directory which is deleted on exit. +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +TMPDIR=`mktemp -d -p "$DIR"` +function cleanup { + rm -rf "${TMPDIR}" +} +trap cleanup EXIT + +curl "${HAURL}/api/onboarding/users" \ + --silent \ + -H 'Content-Type: text/plain;charset=UTF-8' \ + --data-binary "{\"client_id\":\"${HAURL}\",\"name\":\"${HANAME}\",\"username\":\"${HAUSER}\",\"password\":\"${HAPASSWORD}\",\"language\":\"en\"}" \ + --output - \ + | jq -r '.auth_code' > "${TMPDIR}/auth_code" + +curl "${HAURL}/auth/token" \ + --silent \ + -X 'POST' \ + -F "client_id=${HAURL}" \ + -F "code=$(cat "${TMPDIR}/auth_code")" \ + -F 'grant_type=authorization_code' \ + --output - \ + | jq -r '.access_token' > "${TMPDIR}/access_token" + +curl "${HAURL}/api/onboarding/core_config" \ + --silent \ + -X 'POST' \ + -H "Authorization: Bearer $(cat "${TMPDIR}/access_token")" \ + -o /dev/null + +curl "${HAURL}/api/onboarding/analytics" \ + --silent \ + -X 'POST' \ + -H "Authorization: Bearer $(cat "${TMPDIR}/access_token")" \ + -o /dev/null + +curl "${HAURL}/api/onboarding/integration" \ + --silent \ + -H 'Content-Type: application/json;charset=UTF-8' \ + -H "Authorization: Bearer $(cat "${TMPDIR}/access_token")" \ + --data-binary "{\"client_id\":\"${HAURL}\",\"redirect_uri\":\"${HAURL}/?auth_callback=1\"}" \ + -o /dev/null + +# Configure MQTT integration. + +curl "${HAURL}/api/config/config_entries/flow" \ + --silent \ + -X 'POST' \ + -H 'Content-Type: application/json;charset=UTF-8' \ + -H "Authorization: Bearer $(cat "${TMPDIR}/access_token")" \ + --data-binary '{"handler":"mqtt","show_advanced_options":false}' \ + --output - \ + | jq -r '.flow_id' > "${TMPDIR}/flow_id" + +curl "${HAURL}/api/config/config_entries/flow/$(cat "${TMPDIR}/flow_id")" \ + --silent \ + -X 'POST' \ + -H 'Content-Type: application/json;charset=UTF-8' \ + -H "Authorization: Bearer $(cat "${TMPDIR}/access_token")" \ + --data-binary '{"broker":"mosquitto","port":1883}' \ + -o /dev/null + +echo "Created user '${HAUSER}'" diff --git a/test/docker-compose.yml b/test/docker-compose.yml index 5dec39b..71414a6 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -1,7 +1,7 @@ services: homeassistant: container_name: 'ha' - image: 'homeassistant/home-assistant:2022.2.9' + image: 'homeassistant/home-assistant:2023.10.4' volumes: - './ha.yaml:/config/configuration.yaml' ports: @@ -11,7 +11,7 @@ services: mosquitto: container_name: mosquitto - image: eclipse-mosquitto + image: eclipse-mosquitto:2.0.17 volumes: - './mosquitto.conf:/mosquitto/config/mosquitto.conf:ro' ports: diff --git a/test/ha.yaml b/test/ha.yaml index 8448750..84df852 100644 --- a/test/ha.yaml +++ b/test/ha.yaml @@ -5,11 +5,8 @@ homeassistant: name: Wifi Presence Test auth_providers: - type: trusted_networks + allow_bypass_login: true trusted_networks: - 172.0.0.0/8 - 192.0.0.0/8 - allow_bypass_login: true - type: homeassistant - -mqtt: - broker: "mosquitto"