Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

Persist directory registration key

Gabe Kangas9bca56dda645
diff --git a/yp/yp.go b/yp/yp.go
index 76ba74fadf..f12de4f83a 100644
--- a/yp/yp.go
+++ b/yp/yp.go
@@ -23,6 +23,7 @@ const pingInterval = 4 * time.Minute
// YP is a service for handling listing in the Owncast directory.
type YP struct {
timer *time.Ticker
+ post func(string, string, io.Reader) (*http.Response, error)
// getStatus returns the current stream status; consulted on each
// ping cycle to skip pings while offline.
@@ -61,6 +62,7 @@ func New(deps Deps) *YP {
return &YP{
getStatus: deps.GetStatus,
configRepository: deps.ConfigRepository,
+ post: http.Post,
}
}
@@ -148,7 +150,7 @@ func (yp *YP) ping() {
}
pingURL := config.GetDefaults().YPServer + "/api/ping"
- resp, err := http.Post(pingURL, "application/json", bytes.NewBuffer(req)) //nolint
+ resp, err := yp.post(pingURL, "application/json", bytes.NewBuffer(req)) //nolint
if err != nil {
log.Errorln(err)
return
@@ -176,7 +178,7 @@ func (yp *YP) ping() {
yp.inErrorState = false
if pingResponse.Key != key {
- if err := yp.configRepository.SetDirectoryRegistrationKey(key); err != nil {
+ if err := yp.configRepository.SetDirectoryRegistrationKey(pingResponse.Key); err != nil {
log.Errorln("unable to save directory key:", err)
}
}
diff --git a/yp/yp_test.go b/yp/yp_test.go
new file mode 100644
index 0000000000..8d415244ab
--- /dev/null
+++ b/yp/yp_test.go
@@ -0,0 +1,44 @@
+package yp
+
+import (
+ "io"
+ "net/http"
+ "strings"
+ "testing"
+
+ "github.com/owncast/owncast/models"
+ "github.com/owncast/owncast/persistence/configrepository"
+)
+
+type pingConfig struct {
+ configrepository.ConfigRepository
+ storedKey string
+}
+
+func (*pingConfig) GetDirectoryEnabled() bool { return true }
+func (*pingConfig) GetServerURL() string { return "https://example.com" }
+func (*pingConfig) GetServerName() string { return "Test Server" }
+func (c *pingConfig) GetDirectoryRegistrationKey() string { return c.storedKey }
+func (c *pingConfig) SetDirectoryRegistrationKey(key string) error {
+ c.storedKey = key
+ return nil
+}
+
+func TestPingPersistsReturnedRegistrationKey(t *testing.T) {
+ cfg := &pingConfig{storedKey: "old-key"}
+ y := New(Deps{
+ GetStatus: func() models.Status { return models.Status{Online: true} },
+ ConfigRepository: cfg,
+ })
+ y.post = func(string, string, io.Reader) (*http.Response, error) {
+ return &http.Response{
+ Body: io.NopCloser(strings.NewReader(`{"key":"new-key","success":true}`)),
+ }, nil
+ }
+
+ y.ping()
+
+ if cfg.storedKey != "new-key" {
+ t.Fatalf("stored key = %q, want returned key", cfg.storedKey)
+ }
+}