A failed connect from the add/edit form reopens it with the typed values and the error, instead of dropping back to the list. Far more useful when the address is wrong. Bump version to 0.1.4 (build 5).
329 lines
8.9 KiB
Go
329 lines
8.9 KiB
Go
package appui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/egoist/asuka/ui"
|
|
"github.com/egoist/asuka/uitest"
|
|
"github.com/openchamber/openchamber-native/internal/storage"
|
|
)
|
|
|
|
// memStore is a storage.Store that keeps everything in memory.
|
|
type memStore struct {
|
|
mu sync.Mutex
|
|
conns []storage.Connection
|
|
tokens map[string]string
|
|
id string
|
|
}
|
|
|
|
func (m *memStore) Connections() []storage.Connection {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
out := make([]storage.Connection, len(m.conns))
|
|
copy(out, m.conns)
|
|
return out
|
|
}
|
|
|
|
func (m *memStore) SaveConnections(list []storage.Connection) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
out := make([]storage.Connection, len(list))
|
|
copy(out, list)
|
|
m.conns = out
|
|
}
|
|
|
|
func (m *memStore) InstallID() string {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if m.id == "" {
|
|
m.id = "test-install"
|
|
}
|
|
return m.id
|
|
}
|
|
|
|
func (m *memStore) LoadToken(c storage.Connection) (string, bool, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
t, ok := m.tokens[c.ID]
|
|
return t, ok, nil
|
|
}
|
|
|
|
func (m *memStore) SaveToken(id, token string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if m.tokens == nil {
|
|
m.tokens = map[string]string{}
|
|
}
|
|
m.tokens[id] = token
|
|
return nil
|
|
}
|
|
|
|
func (m *memStore) ClearToken(c storage.Connection) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
delete(m.tokens, c.ID)
|
|
return nil
|
|
}
|
|
|
|
func (m *memStore) token(id string) string {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return m.tokens[id]
|
|
}
|
|
|
|
func (m *memStore) connections() []storage.Connection {
|
|
return m.Connections()
|
|
}
|
|
|
|
// fakeServer stands in for OpenChamber: /health, /auth/session and the
|
|
// session list. An empty password makes it passwordless.
|
|
func fakeServer(t *testing.T, password, validToken, sessionTitle string) *httptest.Server {
|
|
t.Helper()
|
|
if sessionTitle == "" {
|
|
sessionTitle = "First session"
|
|
}
|
|
mux := http.NewServeMux()
|
|
authorized := func(r *http.Request) bool {
|
|
if validToken == "" {
|
|
return true
|
|
}
|
|
return r.Header.Get("Authorization") == "Bearer "+validToken
|
|
}
|
|
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(w, `{"status":"ok","openchamberVersion":"2.7.0","isOpenCodeReady":true,"serverId":"srv_1"}`)
|
|
})
|
|
mux.HandleFunc("/auth/session", func(w http.ResponseWriter, r *http.Request) {
|
|
if password == "" {
|
|
fmt.Fprint(w, `{"authenticated":true,"disabled":true}`)
|
|
return
|
|
}
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
if authorized(r) {
|
|
fmt.Fprint(w, `{"authenticated":true,"scope":"client"}`)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
fmt.Fprint(w, `{"authenticated":false,"locked":true}`)
|
|
case http.MethodPost:
|
|
var body struct {
|
|
Password string `json:"password"`
|
|
}
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
if body.Password != password {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
fmt.Fprint(w, `{"error":"Invalid credentials"}`)
|
|
return
|
|
}
|
|
fmt.Fprintf(w, `{"authenticated":true,"clientToken":%q}`, validToken)
|
|
}
|
|
})
|
|
mux.HandleFunc("/api/location", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(w, `{"directory":"/home/me/project","project":{"id":"p1"}}`)
|
|
})
|
|
mux.HandleFunc("/api/session", func(w http.ResponseWriter, r *http.Request) {
|
|
if !authorized(r) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
fmt.Fprintf(w, `{"data":[{"id":"ses_1","title":%q,"time":{"created":1,"updated":2},
|
|
"location":{"directory":"/home/me/project"},"model":{"id":"m","providerID":"p"}}],"cursor":{}}`, sessionTitle)
|
|
})
|
|
mux.HandleFunc("/api/session/active", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(w, `{"data":{"ses_1":{"type":"running"}}}`)
|
|
})
|
|
mux.HandleFunc("/api/permission/request", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(w, `{"data":[]}`)
|
|
})
|
|
mux.HandleFunc("/api/event", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
fmt.Fprint(w, "data: {\"id\":\"evt_1\",\"type\":\"server.connected\",\"data\":{}}\n\n")
|
|
if f, ok := w.(http.Flusher); ok {
|
|
f.Flush()
|
|
}
|
|
<-r.Context().Done()
|
|
})
|
|
srv := httptest.NewServer(mux)
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
func mount(t *testing.T, st *memStore) *uitest.App {
|
|
t.Helper()
|
|
return uitest.Mount(t, func() ui.Node { return rootWithStore(st) }, uitest.Platform(ui.PlatformAndroid))
|
|
}
|
|
|
|
// connectViaForm adds a server from the first screen (or the saved list)
|
|
// and taps Connect.
|
|
func connectViaForm(t *testing.T, a *uitest.App, url string) {
|
|
t.Helper()
|
|
if a.Has("手动输入地址") {
|
|
a.Tap("手动输入地址")
|
|
} else {
|
|
a.Tap("添加服务器")
|
|
}
|
|
a.Type("http://192.168.1.5:3000", url)
|
|
a.Tap("连接")
|
|
}
|
|
|
|
func unlockWith(t *testing.T, a *uitest.App, password string) {
|
|
t.Helper()
|
|
a.WaitFor("这台服务器需要密码。")
|
|
a.Type("密码", password)
|
|
a.Tap("解锁")
|
|
}
|
|
|
|
func TestConnectWithPassword(t *testing.T) {
|
|
srv := fakeServer(t, "secret", "oc_client_test", "")
|
|
st := &memStore{}
|
|
a := mount(t, st)
|
|
connectViaForm(t, a, srv.URL)
|
|
unlockWith(t, a, "secret")
|
|
a.WaitFor("First session")
|
|
|
|
conns := st.connections()
|
|
if len(conns) != 1 || conns[0].URL != srv.URL {
|
|
t.Fatalf("connections = %+v", conns)
|
|
}
|
|
if got := st.token(conns[0].ID); got != "oc_client_test" {
|
|
t.Errorf("token = %q", got)
|
|
}
|
|
if conns[0].Passwordless {
|
|
t.Error("password server marked passwordless")
|
|
}
|
|
}
|
|
|
|
func TestWrongPasswordThenRetry(t *testing.T) {
|
|
srv := fakeServer(t, "secret", "oc_client_test", "")
|
|
st := &memStore{}
|
|
a := mount(t, st)
|
|
connectViaForm(t, a, srv.URL)
|
|
unlockWith(t, a, "nope")
|
|
a.WaitFor("密码错误")
|
|
// The prompt stays up so the password can be retried.
|
|
a.Type("密码", "secret")
|
|
a.Tap("解锁")
|
|
a.WaitFor("First session")
|
|
}
|
|
|
|
func TestAutoReconnectWithStoredToken(t *testing.T) {
|
|
srv := fakeServer(t, "secret", "oc_client_test", "")
|
|
st := &memStore{
|
|
conns: []storage.Connection{{ID: "a", URL: srv.URL, Label: "Home", LastUsedAt: 1}},
|
|
tokens: map[string]string{"a": "oc_client_test"},
|
|
}
|
|
a := mount(t, st)
|
|
a.WaitFor("First session")
|
|
a.ExpectNo("手动输入地址")
|
|
a.ExpectNo("这台服务器需要密码。")
|
|
}
|
|
|
|
func TestExpiredTokenAsksForPassword(t *testing.T) {
|
|
srv := fakeServer(t, "secret", "oc_client_test", "")
|
|
st := &memStore{
|
|
conns: []storage.Connection{{ID: "a", URL: srv.URL, Label: "Home", LastUsedAt: 1}},
|
|
tokens: map[string]string{"a": "revoked"},
|
|
}
|
|
a := mount(t, st)
|
|
a.WaitFor("登录已失效,请重新输入密码")
|
|
a.Until("the revoked token to be dropped", func() bool { return st.token("a") == "" })
|
|
unlockWith(t, a, "secret")
|
|
a.WaitFor("First session")
|
|
}
|
|
|
|
func TestPasswordlessServer(t *testing.T) {
|
|
srv := fakeServer(t, "", "", "")
|
|
st := &memStore{}
|
|
a := mount(t, st)
|
|
connectViaForm(t, a, srv.URL)
|
|
a.WaitFor("First session")
|
|
conns := st.connections()
|
|
if len(conns) != 1 || !conns[0].Passwordless {
|
|
t.Fatalf("connections = %+v", conns)
|
|
}
|
|
if got := st.token(conns[0].ID); got != "" {
|
|
t.Errorf("unexpected token %q", got)
|
|
}
|
|
}
|
|
|
|
func TestMultipleConnectionsAndSwitch(t *testing.T) {
|
|
srvA := fakeServer(t, "", "", "Session A")
|
|
srvB := fakeServer(t, "", "", "Session B")
|
|
st := &memStore{conns: []storage.Connection{
|
|
{ID: "a", URL: srvA.URL, Label: "A", Passwordless: true, LastUsedAt: 2},
|
|
{ID: "b", URL: srvB.URL, Label: "B", Passwordless: true, LastUsedAt: 1},
|
|
}}
|
|
a := mount(t, st)
|
|
a.WaitFor("Session A")
|
|
|
|
a.Tap("断开")
|
|
a.Expect("A")
|
|
a.Expect("B")
|
|
|
|
a.Tap("B")
|
|
a.WaitFor("Session B")
|
|
}
|
|
|
|
func TestAddAnotherConnection(t *testing.T) {
|
|
srvA := fakeServer(t, "", "", "Session A")
|
|
srvB := fakeServer(t, "", "", "Session B")
|
|
st := &memStore{conns: []storage.Connection{
|
|
{ID: "a", URL: srvA.URL, Label: "A", Passwordless: true, LastUsedAt: 1},
|
|
}}
|
|
a := mount(t, st)
|
|
a.WaitFor("Session A")
|
|
a.Tap("断开")
|
|
a.Tap("添加服务器")
|
|
a.Type("http://192.168.1.5:3000", srvB.URL)
|
|
a.Type("名称(可选)", "B")
|
|
a.Tap("连接")
|
|
a.WaitFor("Session B")
|
|
|
|
if conns := st.connections(); len(conns) != 2 {
|
|
t.Fatalf("connections = %+v", conns)
|
|
}
|
|
}
|
|
|
|
func TestDeleteConnection(t *testing.T) {
|
|
srv := fakeServer(t, "", "", "Session A")
|
|
st := &memStore{conns: []storage.Connection{
|
|
{ID: "a", URL: srv.URL, Label: "A", Passwordless: true, LastUsedAt: 1},
|
|
}}
|
|
a := mount(t, st)
|
|
a.WaitFor("Session A")
|
|
a.Tap("断开")
|
|
|
|
a.Tap("操作")
|
|
a.Tap("删除")
|
|
a.Tap("删除")
|
|
a.Until("the connection list to empty", func() bool { return len(st.connections()) == 0 })
|
|
a.Expect("手动输入地址")
|
|
}
|
|
|
|
func TestLabelFallsBackToHost(t *testing.T) {
|
|
c := &controller{}
|
|
conn := storage.Connection{URL: "http://192.168.1.5:3000", Label: "http://192.168.1.5:3000"}
|
|
if got := c.labelOf(conn); got != "192.168.1.5:3000" {
|
|
t.Fatalf("label = %q", got)
|
|
}
|
|
conn.Label = "Home"
|
|
if got := c.labelOf(conn); got != "Home" {
|
|
t.Fatalf("label = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestFailedAddReopensTheForm(t *testing.T) {
|
|
a := mount(t, &memStore{})
|
|
a.Tap("手动输入地址")
|
|
a.Type("http://192.168.1.5:3000", "http://127.0.0.1:1")
|
|
a.Tap("连接")
|
|
a.WaitFor("连接被拒绝,请确认服务器地址与端口")
|
|
a.Expect("添加服务器")
|
|
a.Expect("取消")
|
|
}
|