Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/auth"
"github.com/evcc-io/evcc/util/config"
"github.com/evcc-io/evcc/util/sponsor"
"github.com/evcc-io/evcc/util/telemetry"
"github.com/go-http-utils/etag"
"github.com/gorilla/handlers"
Expand Down Expand Up @@ -223,6 +224,9 @@ func (s *HTTPd) RegisterSiteHandlers(site site.API, valueChan chan<- util.Param)
func (s *HTTPd) RegisterSystemHandler(site *core.Site, valueChan chan<- util.Param, cache *util.ParamCache, auth auth.Auth, shutdown func(), configFile string) {
router := s.Server.Handler.(*mux.Router)

// Initialize sponsor package with UI channel
sponsor.Init(valueChan)

// api
api := router.PathPrefix("/api").Subrouter()
api.Use(jsonHandler)
Expand Down Expand Up @@ -285,9 +289,9 @@ func (s *HTTPd) RegisterSystemHandler(site *core.Site, valueChan chan<- util.Par
"deletedevice": {"DELETE", "/devices/{class:[a-z]+}/{id:[0-9.]+}", deleteDeviceHandler(site)},
"testconfig": {"POST", "/test/{class:[a-z]+}", testConfigHandler},
"testmerged": {"POST", "/test/{class:[a-z]+}/merge/{id:[0-9.]+}", testConfigHandler},
"interval": {"POST", "/interval/{value:[0-9.]+}", settingsSetDurationHandler(keys.Interval)},
"updatesponsortoken": {"POST", "/sponsortoken", updateSponsortokenHandler},
"deletesponsortoken": {"DELETE", "/sponsortoken", deleteSponsorTokenHandler},
"interval": {"POST", "/interval/{value:[0-9.]+}", settingsSetDurationHandler(keys.Interval, valueChan)},
"updatesponsortoken": {"POST", "/sponsortoken", updateSponsortokenHandler()},
"deletesponsortoken": {"DELETE", "/sponsortoken", deleteSponsorTokenHandler()},
}

// yaml handlers
Expand Down
40 changes: 17 additions & 23 deletions server/http_config_site_other_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,32 @@ import (
"encoding/json"
"net/http"

"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/server/db/settings"
"github.com/evcc-io/evcc/util/sponsor"
)

func updateSponsortokenHandler(w http.ResponseWriter, r *http.Request) {
var req struct {
Token string `json:"token"`
}

if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
func updateSponsortokenHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Token string `json:"token"`
}

if req.Token != "" {
if err := sponsor.ConfigureSponsorship(req.Token); err != nil {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
sponsor.SetFromYaml(false)
}

// TODO find better place
settings.SetString(keys.SponsorToken, req.Token)
setConfigDirty()
if err := sponsor.SaveToken(req.Token, setConfigDirty); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}

jsonWrite(w, sponsor.Status())
jsonWrite(w, sponsor.Status())
}
}

func deleteSponsorTokenHandler(w http.ResponseWriter, r *http.Request) {
settings.SetString(keys.SponsorToken, "")
setConfigDirty()
jsonWrite(w, true)
func deleteSponsorTokenHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sponsor.DeleteToken(setConfigDirty)
jsonWrite(w, true)
}
}
4 changes: 3 additions & 1 deletion server/http_global_settings_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func settingsDeleteHandler(key string) http.HandlerFunc {
}
}

func settingsSetDurationHandler(key string) http.HandlerFunc {
func settingsSetDurationHandler(key string, valueChan chan<- util.Param) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

Expand All @@ -41,6 +41,8 @@ func settingsSetDurationHandler(key string) http.HandlerFunc {
settings.SetInt(key, int64(time.Second*time.Duration(val)))
setConfigDirty()

valueChan <- util.Param{Key: key, Val: val}

jsonWrite(w, val)
}
}
Expand Down
68 changes: 68 additions & 0 deletions tests/config-control.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { test, expect } from "@playwright/test";
import { start, stop, baseUrl } from "./evcc";
import { enableExperimental, expectModalVisible, expectModalHidden } from "./utils";

test.use({ baseURL: baseUrl() });

test.afterEach(async () => {
await stop();
});

test.describe("control settings (interval)", () => {
test("interval is immediately visible after save without restart", async ({ page }) => {
await start();
await page.goto("/#/config");
await enableExperimental(page);

const entry = page.getByTestId("generalconfig-control");
await expect(entry).toContainText("10 s");

await entry.getByRole("button", { name: "Edit" }).click();

const modal = page.getByTestId("control-modal");
await expectModalVisible(modal);

const intervalInput = modal.getByLabel("Interval");
await expect(intervalInput).toHaveValue("10");
await intervalInput.fill("20");

await modal.getByRole("button", { name: "Save" }).click();

await expectModalHidden(modal);

await expect(page.getByTestId("restart-needed")).toBeVisible();

await expect(entry).toContainText("20 s");
await expect(entry).not.toContainText("10 s");

await entry.getByRole("button", { name: "Edit" }).click();
await expectModalVisible(modal);
await expect(intervalInput).toHaveValue("20");
});

test("residual power is immediately visible after save", async ({ page }) => {
await start();
await page.goto("/#/config");
await enableExperimental(page);

const entry = page.getByTestId("generalconfig-control");
await entry.getByRole("button", { name: "Edit" }).click();

const modal = page.getByTestId("control-modal");
await expectModalVisible(modal);

const residualPowerInput = modal.getByLabel("Residual power");
const initialValue = await residualPowerInput.inputValue();

await residualPowerInput.fill("200");

await modal.getByRole("button", { name: "Save" }).click();

await expectModalHidden(modal);

await entry.getByRole("button", { name: "Edit" }).click();
await expectModalVisible(modal);
await expect(residualPowerInput).toHaveValue("200");
await expect(residualPowerInput).not.toHaveValue(initialValue);
});
});
16 changes: 7 additions & 9 deletions tests/sponsor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ test.describe("sponsor token", () => {
await expect(page.getByTestId("fatal-error")).toContainText("sponsorship");

// Open sponsor modal
const sponsorEntry = page.getByTestId("generalconfig-sponsoring");
await expect(sponsorEntry).toContainText("invalid");
await sponsorEntry.getByRole("button", { name: "Edit" }).click();
const entry = page.getByTestId("generalconfig-sponsoring");
await expect(entry).toContainText("invalid");
await entry.getByRole("button", { name: "Edit" }).click();

const modal = page.getByTestId("sponsor-modal");
const tokenInput = modal.getByRole("textbox", { name: "Your token" });
Expand Down Expand Up @@ -63,17 +63,15 @@ test.describe("sponsor token", () => {
await page.goto("/#/config");
await enableExperimental(page);

// Open sponsor modal and enter token
await page
.getByTestId("generalconfig-sponsoring")
.getByRole("button", { name: "Edit" })
.click();
const entry = page.getByTestId("generalconfig-sponsoring");
await expect(entry).toContainText("---");

await entry.getByRole("button", { name: "Edit" }).click();

const modal = page.getByTestId("sponsor-modal");
const textarea = modal.getByRole("textbox", { name: "Enter your token" });

await textarea.fill(EXPIRED_TOKEN);
// Try to save to trigger validation
await modal.getByRole("button", { name: "Save" }).click();
await expect(modal).toContainText("token is expired");
});
Expand Down
40 changes: 40 additions & 0 deletions util/sponsor/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"time"

"github.com/evcc-io/evcc/api/proto/pb"
"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/server/db/settings"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/cloud"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -17,6 +20,7 @@ var (
Subject, Token string
fromYaml bool = true
ExpiresAt time.Time
uiChan chan<- util.Param
)

const (
Expand Down Expand Up @@ -124,3 +128,39 @@ func Status() sponsorStatus {
FromYaml: fromYaml,
}
}

// Init initializes the sponsor package with the UI channel for publishing updates
func Init(ch chan<- util.Param) {
mu.Lock()
defer mu.Unlock()
uiChan = ch
}

// SaveToken saves the sponsor token to database and publishes status update
func SaveToken(token string, setDirty func()) error {
if token != "" {
if err := ConfigureSponsorship(token); err != nil {
return err
}
SetFromYaml(false)
}

settings.SetString(keys.SponsorToken, token)
setDirty()

publishStatus()
return nil
}

// DeleteToken removes the sponsor token from database and publishes status update
func DeleteToken(setDirty func()) {
settings.SetString(keys.SponsorToken, "")
setDirty()
publishStatus()
}

func publishStatus() {
if uiChan != nil {
uiChan <- util.Param{Key: keys.Sponsor, Val: Status()}
}
}