The stored connection remembers that no password was set, but the reconnect path still required a device token and asked for the password. Reconnect without one when the server issued none.
237 lines
6.1 KiB
Go
237 lines
6.1 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
|
|
conn storage.Connection
|
|
has bool
|
|
tokens map[string]string
|
|
id string
|
|
}
|
|
|
|
func (m *memStore) Connection() (storage.Connection, bool) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return m.conn, m.has
|
|
}
|
|
func (m *memStore) SaveConnection(c storage.Connection) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.conn, m.has = c, c.URL != ""
|
|
}
|
|
func (m *memStore) ClearConnection() {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.conn, m.has = storage.Connection{}, false
|
|
}
|
|
func (m *memStore) InstallID() string {
|
|
if m.id == "" {
|
|
m.id = "test-install"
|
|
}
|
|
return m.id
|
|
}
|
|
func (m *memStore) LoadToken(server string) (string, bool, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
t, ok := m.tokens[server]
|
|
return t, ok, nil
|
|
}
|
|
func (m *memStore) SaveToken(server, token string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if m.tokens == nil {
|
|
m.tokens = map[string]string{}
|
|
}
|
|
m.tokens[server] = token
|
|
return nil
|
|
}
|
|
func (m *memStore) ClearToken(server string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
delete(m.tokens, server)
|
|
return nil
|
|
}
|
|
|
|
func (m *memStore) token(server string) string {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return m.tokens[server]
|
|
}
|
|
|
|
func (m *memStore) connection() storage.Connection {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return m.conn
|
|
}
|
|
|
|
// fakeServer stands in for OpenChamber: /health, /auth/session and the
|
|
// session list. password "" with validToken "" makes it passwordless.
|
|
func fakeServer(t *testing.T, password, validToken string) *httptest.Server {
|
|
t.Helper()
|
|
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/session", func(w http.ResponseWriter, r *http.Request) {
|
|
if !authorized(r) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
fmt.Fprint(w, `{"data":[
|
|
{"id":"ses_1","title":"First session","time":{"created":1,"updated":2},
|
|
"location":{"directory":"/home/me/project"},"model":{"id":"m","providerID":"p"}}],
|
|
"cursor":{}}`)
|
|
})
|
|
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))
|
|
}
|
|
|
|
func TestConnectWithPassword(t *testing.T) {
|
|
srv := fakeServer(t, "secret", "oc_client_test")
|
|
|
|
st := &memStore{}
|
|
a := mount(t, st)
|
|
a.Expect("连接到 OpenChamber")
|
|
|
|
a.Type("http://192.168.1.5:3000", srv.URL)
|
|
a.Type("密码(可选)", "secret")
|
|
a.Tap("连接")
|
|
a.WaitFor("First session")
|
|
a.ExpectNo("密码(可选)")
|
|
|
|
if got := st.connection().URL; got != srv.URL {
|
|
t.Errorf("connection not saved: %q", got)
|
|
}
|
|
if got := st.token(srv.URL); got != "oc_client_test" {
|
|
t.Errorf("token not saved: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestWrongPassword(t *testing.T) {
|
|
srv := fakeServer(t, "secret", "oc_client_test")
|
|
|
|
a := mount(t, &memStore{})
|
|
a.Type("http://192.168.1.5:3000", srv.URL)
|
|
a.Type("密码(可选)", "nope")
|
|
a.Tap("连接")
|
|
a.WaitFor("密码错误")
|
|
}
|
|
|
|
func TestAutoReconnectWithStoredToken(t *testing.T) {
|
|
srv := fakeServer(t, "secret", "oc_client_test")
|
|
|
|
st := &memStore{
|
|
conn: storage.Connection{URL: srv.URL},
|
|
has: true,
|
|
tokens: map[string]string{srv.URL: "oc_client_test"},
|
|
}
|
|
a := mount(t, st)
|
|
a.WaitFor("First session")
|
|
a.ExpectNo("连接到 OpenChamber")
|
|
}
|
|
|
|
func TestExpiredTokenAsksForPassword(t *testing.T) {
|
|
srv := fakeServer(t, "secret", "oc_client_test")
|
|
|
|
st := &memStore{
|
|
conn: storage.Connection{URL: srv.URL},
|
|
has: true,
|
|
tokens: map[string]string{srv.URL: "revoked"},
|
|
}
|
|
a := mount(t, st)
|
|
a.WaitFor("登录已失效,请输入密码重新连接")
|
|
a.Expect("连接到 OpenChamber")
|
|
a.Until("the revoked token to be dropped", func() bool { return st.token(srv.URL) == "" })
|
|
}
|
|
|
|
func TestPasswordlessServer(t *testing.T) {
|
|
srv := fakeServer(t, "", "")
|
|
|
|
st := &memStore{}
|
|
a := mount(t, st)
|
|
a.Type("http://192.168.1.5:3000", srv.URL)
|
|
a.Tap("连接")
|
|
a.WaitFor("First session")
|
|
if got := st.connection(); !got.Passwordless {
|
|
t.Errorf("passwordless not remembered: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestAutoReconnectPasswordlessWithoutToken(t *testing.T) {
|
|
srv := fakeServer(t, "", "")
|
|
t.Cleanup(srv.Close)
|
|
|
|
st := &memStore{
|
|
conn: storage.Connection{URL: srv.URL, Passwordless: true},
|
|
has: true,
|
|
}
|
|
a := mount(t, st)
|
|
a.WaitFor("First session")
|
|
a.ExpectNo("请重新输入密码")
|
|
}
|