2020-12-04 13:10:32 +01:00
|
|
|
package hub
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
log = logrus.StandardLogger()
|
2022-03-02 12:20:02 +01:00
|
|
|
log.SetLevel(logrus.TraceLevel)
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
ServiceURL = "https://hub-int.photoprism.app/v1/hello"
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Token returns a random token with length of up to 10 characters.
|
|
|
|
func Token(size uint) string {
|
|
|
|
if size > 10 || size < 1 {
|
|
|
|
panic(fmt.Sprintf("size out of range: %d", size))
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]byte, 0, 14)
|
|
|
|
b := make([]byte, 8)
|
|
|
|
|
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
randomInt := binary.BigEndian.Uint64(b)
|
|
|
|
|
|
|
|
result = append(result, strconv.FormatUint(randomInt, 36)...)
|
|
|
|
|
|
|
|
for i := len(result); i < cap(result); i++ {
|
|
|
|
result = append(result, byte(123-(cap(result)-i)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(result[:size])
|
|
|
|
}
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
func TestNewConfig(t *testing.T) {
|
2022-03-02 14:16:49 +01:00
|
|
|
c := NewConfig("0.0.0", "testdata/new.yml", "zqkunt22r0bewti9", "test", "PhotoPrism/Test", "test")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
assert.IsType(t, &Config{}, c)
|
|
|
|
}
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
func TestNewRequest(t *testing.T) {
|
2022-03-02 14:16:49 +01:00
|
|
|
r := NewRequest("0.0.0", "zqkunt22r0bewti9", "test", "test")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
assert.IsType(t, &Request{}, r)
|
|
|
|
|
|
|
|
t.Logf("Request: %+v", r)
|
|
|
|
|
|
|
|
if j, err := json.Marshal(r); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
t.Logf("JSON: %s", j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_Refresh(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
2020-12-04 13:10:32 +01:00
|
|
|
fileName := fmt.Sprintf("testdata/hub.%s.yml", Token(8))
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2022-03-02 14:16:49 +01:00
|
|
|
c := NewConfig("0.0.0", fileName, "zqkunt22r0bewti9", "test", "PhotoPrism/Test", "test")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
if err := c.Refresh(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, c.Key, 40)
|
|
|
|
assert.Len(t, c.Secret, 32)
|
2020-11-16 19:39:40 +01:00
|
|
|
assert.Equal(t, "0.0.0", c.Version)
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
if sess, err := c.DecodeSession(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if sess.Expired() {
|
|
|
|
t.Fatalf("session expired: %+v", sess)
|
|
|
|
}
|
|
|
|
|
2020-10-07 12:22:45 +02:00
|
|
|
if err := c.Save(); err != nil {
|
2020-10-03 13:50:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.Remove(fileName)
|
|
|
|
|
|
|
|
assert.FileExists(t, fileName)
|
|
|
|
|
|
|
|
if err := c.Refresh(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, c.Key, 40)
|
|
|
|
assert.Len(t, c.Secret, 32)
|
2020-11-16 19:39:40 +01:00
|
|
|
assert.Equal(t, "0.0.0", c.Version)
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
if sess, err := c.DecodeSession(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if sess.Expired() {
|
|
|
|
t.Fatal("session expired")
|
2020-10-04 04:47:54 +02:00
|
|
|
} else {
|
|
|
|
t.Logf("api session: %+v", sess)
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 12:22:45 +02:00
|
|
|
if err := c.Save(); err != nil {
|
2020-10-03 13:50:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.FileExists(t, fileName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_DecodeSession(t *testing.T) {
|
2020-12-04 13:10:32 +01:00
|
|
|
t.Run("hub3.yml", func(t *testing.T) {
|
2022-03-02 14:16:49 +01:00
|
|
|
c := NewConfig("0.0.0", "testdata/hub3.yml", "zqkunt22r0bewti9", "test", "PhotoPrism/Test", "test")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-10-07 12:47:12 +02:00
|
|
|
err := c.Load()
|
|
|
|
|
|
|
|
assert.EqualError(t, err, "session expired")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
assert.Equal(t, "8dd8b115d052f91ac74b1c2475e305009366c487", c.Key)
|
|
|
|
assert.Equal(t, "ddf4ce46afbf6c16a6bd8555ab1e4efb", c.Secret)
|
|
|
|
assert.Equal(t, "7607796238c26b2d95007957b05c72d63f504346576bc2aa064a6dc54344de47d2ab38422bd1d061c067a16ef517e6054d8b7f5336c120431935518277fed45e49472aaf740cac1bc33ab2e362c767007a59e953e9973709", c.Session)
|
|
|
|
assert.Equal(t, "unregistered", c.Status)
|
2020-11-16 19:39:40 +01:00
|
|
|
assert.Equal(t, "0.0.0", c.Version)
|
2020-10-03 13:50:30 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_Load(t *testing.T) {
|
2020-12-04 13:10:32 +01:00
|
|
|
t.Run("hub1.yml", func(t *testing.T) {
|
2022-03-02 14:16:49 +01:00
|
|
|
c := NewConfig("0.0.0", "testdata/hub1.yml", "zqkunt22r0bewti9", "test", "PhotoPrism/Test", "test")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-10-07 12:22:45 +02:00
|
|
|
if err := c.Load(); err != nil {
|
2020-10-13 23:13:18 +02:00
|
|
|
t.Logf(err.Error())
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "b32e9ccdc90eb7c0f6f1b9fbc82b8a2b0e993304", c.Key)
|
|
|
|
assert.Equal(t, "5991ea36a9611e9e00a8360c10b91567", c.Secret)
|
|
|
|
assert.Equal(t, "3ef5685c6391a568731c8fc94ccad82d92dea60476c8b672990047c822248f45366fc0e8e812ad15e0b5ae1eb20e866235c56b", c.Session)
|
|
|
|
assert.Equal(t, "unregistered", c.Status)
|
2020-11-16 19:39:40 +01:00
|
|
|
assert.Equal(t, "0.0.0", c.Version)
|
2020-10-03 13:50:30 +02:00
|
|
|
})
|
2020-12-04 13:10:32 +01:00
|
|
|
t.Run("hub2.yml", func(t *testing.T) {
|
2022-03-02 14:16:49 +01:00
|
|
|
c := NewConfig("0.0.0", "testdata/hub2.yml", "zqkunt22r0bewti9", "test", "PhotoPrism/Test", "test")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-10-07 12:22:45 +02:00
|
|
|
if err := c.Load(); err != nil {
|
2020-10-13 23:13:18 +02:00
|
|
|
t.Logf(err.Error())
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "ab66cb5cfb3658dbea0a1433df048d900934ac68", c.Key)
|
|
|
|
assert.Equal(t, "6b0f8440fe307d3120b3a4366350094b", c.Secret)
|
|
|
|
assert.Equal(t, "c0ca88fc3094b70a1947b5b10f980a420cd6b1542a20f6f26ecc6a16f340473b9fb16b80be1078e86d886b3a8d46bf8184d147", c.Session)
|
|
|
|
assert.Equal(t, "unregistered", c.Status)
|
|
|
|
assert.Equal(t, "200925-f8e2b580-Darwin-i386-DEBUG", c.Version)
|
|
|
|
})
|
|
|
|
t.Run("not existing filename", func(t *testing.T) {
|
2022-03-02 14:16:49 +01:00
|
|
|
c := NewConfig("0.0.0", "testdata/hub_xxx.yml", "zqkunt22r0bewti9", "test", "PhotoPrism/Test", "test")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-10-07 12:22:45 +02:00
|
|
|
if err := c.Load(); err == nil {
|
2020-10-03 13:50:30 +02:00
|
|
|
t.Fatal("file should not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "", c.Key)
|
|
|
|
assert.Equal(t, "", c.Secret)
|
|
|
|
assert.Equal(t, "", c.Session)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_Save(t *testing.T) {
|
|
|
|
t.Run("existing filename", func(t *testing.T) {
|
2020-12-04 13:10:32 +01:00
|
|
|
assert.FileExists(t, "testdata/hub1.yml")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2022-03-02 14:16:49 +01:00
|
|
|
c := NewConfig("0.0.0", "testdata/hub1.yml", "zqkunt22r0bewti9", "test", "PhotoPrism/Test", "test")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-10-07 12:22:45 +02:00
|
|
|
if err := c.Load(); err != nil {
|
2020-10-13 23:13:18 +02:00
|
|
|
t.Logf(err.Error())
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "b32e9ccdc90eb7c0f6f1b9fbc82b8a2b0e993304", c.Key)
|
|
|
|
assert.Equal(t, "5991ea36a9611e9e00a8360c10b91567", c.Secret)
|
|
|
|
assert.Equal(t, "3ef5685c6391a568731c8fc94ccad82d92dea60476c8b672990047c822248f45366fc0e8e812ad15e0b5ae1eb20e866235c56b", c.Session)
|
|
|
|
assert.Equal(t, "unregistered", c.Status)
|
2020-11-16 19:39:40 +01:00
|
|
|
assert.Equal(t, "0.0.0", c.Version)
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
c.FileName = "testdata/hub-save.yml"
|
2020-10-07 12:22:45 +02:00
|
|
|
|
|
|
|
if err := c.Save(); err != nil {
|
2020-10-03 13:50:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
defer os.Remove("testdata/hub-save.yml")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
assert.Equal(t, "b32e9ccdc90eb7c0f6f1b9fbc82b8a2b0e993304", c.Key)
|
|
|
|
assert.Equal(t, "5991ea36a9611e9e00a8360c10b91567", c.Secret)
|
|
|
|
assert.Equal(t, "3ef5685c6391a568731c8fc94ccad82d92dea60476c8b672990047c822248f45366fc0e8e812ad15e0b5ae1eb20e866235c56b", c.Session)
|
|
|
|
assert.Equal(t, "unregistered", c.Status)
|
2020-11-16 19:39:40 +01:00
|
|
|
assert.Equal(t, "0.0.0", c.Version)
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
assert.FileExists(t, "testdata/hub-save.yml")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-10-07 12:22:45 +02:00
|
|
|
if err := c.Load(); err != nil {
|
2020-10-13 23:13:18 +02:00
|
|
|
t.Logf(err.Error())
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "b32e9ccdc90eb7c0f6f1b9fbc82b8a2b0e993304", c.Key)
|
|
|
|
assert.Equal(t, "5991ea36a9611e9e00a8360c10b91567", c.Secret)
|
|
|
|
assert.Equal(t, "3ef5685c6391a568731c8fc94ccad82d92dea60476c8b672990047c822248f45366fc0e8e812ad15e0b5ae1eb20e866235c56b", c.Session)
|
|
|
|
assert.Equal(t, "unregistered", c.Status)
|
2020-11-16 19:39:40 +01:00
|
|
|
assert.Equal(t, "0.0.0", c.Version)
|
2020-10-03 13:50:30 +02:00
|
|
|
})
|
|
|
|
t.Run("not existing filename", func(t *testing.T) {
|
2022-03-02 14:16:49 +01:00
|
|
|
c := NewConfig("0.0.0", "testdata/hub_new.yml", "zqkunt22r0bewti9", "test", "PhotoPrism/Test", "test")
|
2020-10-03 13:50:30 +02:00
|
|
|
c.Key = "F60F5B25D59C397989E3CD374F81CDD7710A4FCA"
|
|
|
|
c.Secret = "foo"
|
|
|
|
c.Session = "bar"
|
|
|
|
|
|
|
|
assert.Equal(t, "F60F5B25D59C397989E3CD374F81CDD7710A4FCA", c.Key)
|
|
|
|
assert.Equal(t, "foo", c.Secret)
|
|
|
|
assert.Equal(t, "bar", c.Session)
|
|
|
|
|
2020-10-07 12:22:45 +02:00
|
|
|
if err := c.Save(); err != nil {
|
2020-10-03 13:50:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "", c.Key)
|
|
|
|
assert.Equal(t, "", c.Secret)
|
|
|
|
assert.Equal(t, "", c.Session)
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
assert.FileExists(t, "testdata/hub_new.yml")
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
if err := os.Remove("testdata/hub_new.yml"); err != nil {
|
2020-10-03 13:50:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|