feat: sign in to IMVU for Access Pass products (#4)
Co-authored-by: dhkatz <[email protected]>
This commit was merged in pull request #4.
This commit is contained in:
1 parent
dd8205602c
commit
145ebf25c1
11 files changed
+1001
-24
No files matched your search
@@ -15,6 +15,8 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -29,20 +31,24 @@ import (
|
||||
|
||||
// fakeHub is the hub's importer API, as t5de-hub's docs/products.md has it, and a catalog's upload.
|
||||
type fakeHub struct {
|
||||
t *testing.T
|
||||
mu sync.Mutex
|
||||
cfg hub.Config
|
||||
queue []hub.Job
|
||||
claims int
|
||||
hello map[string]string
|
||||
reports map[string]hub.Outcome
|
||||
done map[string]map[string][]byte // job → the uploaded product's files
|
||||
account map[string]hub.AccountReport
|
||||
catalog *httptest.Server
|
||||
t *testing.T
|
||||
mu sync.Mutex
|
||||
cfg hub.Config
|
||||
queue []hub.Job
|
||||
claims int
|
||||
hello map[string]string
|
||||
reports map[string]hub.Outcome
|
||||
done map[string]map[string][]byte // job → the uploaded product's files
|
||||
account map[string]hub.AccountReport
|
||||
reportsOf map[string][]hub.AccountReport // every report, by account
|
||||
listings map[string][]hub.Seen // "list" jobs' products
|
||||
infos map[string]*hub.Seen // what the importer saw of jobs' products
|
||||
catalog *httptest.Server
|
||||
}
|
||||
|
||||
func newFakeHub(t *testing.T) (*fakeHub, *httptest.Server) {
|
||||
h := &fakeHub{t: t, reports: map[string]hub.Outcome{}, done: map[string]map[string][]byte{}, account: map[string]hub.AccountReport{}}
|
||||
h := &fakeHub{t: t, reports: map[string]hub.Outcome{}, done: map[string]map[string][]byte{}, account: map[string]hub.AccountReport{},
|
||||
reportsOf: map[string][]hub.AccountReport{}, listings: map[string][]hub.Seen{}, infos: map[string]*hub.Seen{}}
|
||||
h.catalog = httptest.NewServer(http.HandlerFunc(h.upload))
|
||||
t.Cleanup(h.catalog.Close)
|
||||
s := httptest.NewServer(http.HandlerFunc(h.serve))
|
||||
@@ -82,6 +88,26 @@ func (h *fakeHub) serve(w http.ResponseWriter, r *http.Request) {
|
||||
"token": fmt.Sprintf("import:%s:%v", job, body["imvuRevision"]),
|
||||
"url": h.catalog.URL + "/api/v1/uploads",
|
||||
})
|
||||
case strings.HasSuffix(path, "/listing"):
|
||||
var l struct{ Products []hub.Seen }
|
||||
b, _ := json.Marshal(body)
|
||||
json.Unmarshal(b, &l)
|
||||
job := strings.TrimSuffix(strings.TrimPrefix(path, "/jobs/"), "/listing")
|
||||
h.listings[job] = l.Products
|
||||
json.NewEncoder(w).Encode(map[string]int{"listed": len(l.Products), "queued": len(l.Products)})
|
||||
case strings.HasSuffix(path, "/info"):
|
||||
job := strings.TrimSuffix(strings.TrimPrefix(path, "/jobs/"), "/info")
|
||||
answer := map[string]any{"rating": nil, "covered": false}
|
||||
if body["found"] == true {
|
||||
var p hub.Seen
|
||||
b, _ := json.Marshal(body)
|
||||
json.Unmarshal(b, &p)
|
||||
h.infos[job] = &p
|
||||
answer = map[string]any{"rating": p.Rating, "covered": p.CreatorID == 903}
|
||||
} else {
|
||||
h.infos[job] = nil
|
||||
}
|
||||
json.NewEncoder(w).Encode(answer)
|
||||
case strings.HasPrefix(path, "/jobs/"):
|
||||
var o hub.Outcome
|
||||
b, _ := json.Marshal(body)
|
||||
@@ -93,6 +119,14 @@ func (h *fakeHub) serve(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := json.Marshal(body)
|
||||
json.Unmarshal(b, &a)
|
||||
h.account[strings.TrimPrefix(path, "/accounts/")] = a
|
||||
h.reportsOf[strings.TrimPrefix(path, "/accounts/")] = append(h.reportsOf[strings.TrimPrefix(path, "/accounts/")], a)
|
||||
if a.Received { // the hub drops its sealed copy
|
||||
for i := range h.cfg.Accounts {
|
||||
if h.cfg.Accounts[i].ID == strings.TrimPrefix(path, "/accounts/") {
|
||||
h.cfg.Accounts[i].Sealed = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
w.Write([]byte(`{}`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
@@ -130,7 +164,7 @@ func (h *fakeHub) upload(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *fakeHub) settled(n int) bool {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return len(h.reports)+len(h.done) >= n
|
||||
return len(h.reports)+len(h.done)+len(h.listings) >= n
|
||||
}
|
||||
|
||||
func sealFor(t *testing.T, k *keys.Key, imvuID int64, password string) *string {
|
||||
@@ -159,6 +193,10 @@ func newImporterIn(t *testing.T, h *httptest.Server, cdn *imvutest.CDN, dir stri
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sessions := filepath.Join(dir, "sessions")
|
||||
if err := os.MkdirAll(sessions, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var logs bytes.Buffer
|
||||
t.Cleanup(func() {
|
||||
if t.Failed() {
|
||||
@@ -173,6 +211,7 @@ func newImporterIn(t *testing.T, h *httptest.Server, cdn *imvutest.CDN, dir stri
|
||||
Version: "1.2.3",
|
||||
UserAgent: "test",
|
||||
Tmp: t.TempDir(),
|
||||
Sessions: sessions,
|
||||
Logger: log.New(&logs, "", 0),
|
||||
ConfigEvery: 50 * time.Millisecond,
|
||||
ClaimEvery: 10 * time.Millisecond,
|
||||
@@ -247,7 +286,7 @@ func TestImports(t *testing.T) {
|
||||
if o := h.reports["b"]; o.Status != "unchanged" || o.IMVURevision != 3 {
|
||||
t.Errorf("job b: %+v", o)
|
||||
}
|
||||
if o := h.reports["c"]; o.Status != "failed" || !strings.Contains(o.Reason, "rating") {
|
||||
if o := h.reports["c"]; o.Status != "failed" || !strings.Contains(o.Reason, "no IMVU account is signed in") {
|
||||
t.Errorf("job c: %+v", o)
|
||||
}
|
||||
if o := h.reports["d"]; o.Status != "failed" || !strings.Contains(o.Reason, "no revision") {
|
||||
@@ -266,11 +305,12 @@ func TestAccountsAndPause(t *testing.T) {
|
||||
if err := im.Accounts.Put(accounts.Account{ID: "gone", IMVUID: 1, Sealed: "x"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
okSealed := sealFor(t, im.Key, 100, "pw")
|
||||
h.cfg = hub.Config{
|
||||
Paused: true,
|
||||
Settings: hub.Settings{Rate: 1, Concurrency: 1},
|
||||
Accounts: []hub.Account{
|
||||
{ID: "ok", IMVUID: 100, Username: "Importer1", Sealed: sealFor(t, im.Key, 100, "pw")},
|
||||
{ID: "ok", IMVUID: 100, Username: "Importer1", Sealed: okSealed},
|
||||
{ID: "moved", IMVUID: 200, Username: "Importer2", Sealed: sealFor(t, im.Key, 999, "pw")},
|
||||
{ID: "waiting", IMVUID: 300, Username: "Importer3"},
|
||||
},
|
||||
@@ -284,7 +324,7 @@ func TestAccountsAndPause(t *testing.T) {
|
||||
if !h.account["ok"].Received || h.account["moved"].State != "needs_password" {
|
||||
t.Errorf("account reports %+v", h.account)
|
||||
}
|
||||
if a, ok := im.Accounts.Get("ok"); !ok || a.IMVUID != 100 || a.Sealed != *h.cfg.Accounts[0].Sealed {
|
||||
if a, ok := im.Accounts.Get("ok"); !ok || a.IMVUID != 100 || a.Sealed != *okSealed {
|
||||
t.Errorf("kept %+v, %v", a, ok)
|
||||
}
|
||||
if _, ok := im.Accounts.Get("gone"); ok {
|
||||
@@ -317,3 +357,98 @@ func TestHelloRefused(t *testing.T) {
|
||||
t.Error("ran with a refused key")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignedIn(t *testing.T) {
|
||||
api := imvutest.NewAPI(t)
|
||||
api.Users = []imvutest.User{
|
||||
{ID: 777, Username: "ImporterAP", Password: "right", AccessPass: true},
|
||||
{ID: 778, Username: "NoLuck", Password: "right"},
|
||||
{ID: 903, Username: "apkeeperAv"},
|
||||
}
|
||||
api.Products = []imvutest.Product{
|
||||
{ID: 9501, Name: "Coat", CreatorID: 903},
|
||||
{ID: 9502, Name: "Robe", CreatorID: 903, AccessPass: true},
|
||||
}
|
||||
cdn := imvutest.New(t)
|
||||
cdn.Add(9502, 1, imvutest.File{Name: "index.xml", Data: imvutest.Index(5000)})
|
||||
|
||||
h, srv := newFakeHub(t)
|
||||
dir := t.TempDir()
|
||||
im := newImporterIn(t, srv, cdn, dir)
|
||||
im.APIURL = api.URL
|
||||
h.cfg = hub.Config{
|
||||
Settings: hub.Settings{Rate: 10, Concurrency: 1},
|
||||
Accounts: []hub.Account{
|
||||
{ID: "wrong", IMVUID: 778, Username: "NoLuck", Sealed: sealFor(t, im.Key, 778, "not the password")},
|
||||
{ID: "ap", IMVUID: 777, Username: "ImporterAP", Sealed: sealFor(t, im.Key, 777, "right")},
|
||||
},
|
||||
}
|
||||
h.queue = []hub.Job{
|
||||
{ID: "list", Kind: "list", IMVUCreator: 903},
|
||||
{ID: "robe", Kind: "import", IMVUProduct: 9502}, // hidden from the hub: no rating
|
||||
{ID: "gone", Kind: "import", IMVUProduct: 9599},
|
||||
}
|
||||
runUntil(t, im, func() bool { return h.settled(3) })
|
||||
|
||||
// The first account is refused; the second signs in, with Access Pass (which IMVU shows it alone).
|
||||
if r := h.account["wrong"]; r.State != "sign_in_failed" || !strings.Contains(r.Message, "Invalid username or password") {
|
||||
t.Errorf("refused account: %+v", r)
|
||||
}
|
||||
if r := h.account["ap"]; r.State != "ok" || r.AccessPass == nil || !*r.AccessPass {
|
||||
t.Errorf("signed-in account: %+v", r)
|
||||
}
|
||||
// The shop as the account lists it: the Access Pass product too.
|
||||
if got := h.listings["list"]; len(got) != 2 || got[1] != (hub.Seen{ID: 9502, Name: "Robe", Rating: "adult", CreatorID: 903, CreatorName: "creator903", Purchasable: true}) {
|
||||
t.Errorf("listing %+v", got)
|
||||
}
|
||||
// The hidden product: looked up, reported, imported as adult.
|
||||
if p := h.infos["robe"]; p == nil || p.Rating != "adult" || p.CreatorID != 903 {
|
||||
t.Errorf("robe info %+v", p)
|
||||
}
|
||||
if robe := h.done["robe"]; robe == nil || !bytes.Contains(robe["index.xml"], []byte("<ProductRating>AP</ProductRating>")) {
|
||||
t.Errorf("robe: %q (reports %v)", robe, h.reports)
|
||||
}
|
||||
if p, ok := h.infos["gone"]; !ok || p != nil {
|
||||
t.Errorf("gone info %+v, %v", p, ok)
|
||||
}
|
||||
if o := h.reports["gone"]; o.Status != "failed" || !strings.Contains(o.Reason, "deleted?") {
|
||||
t.Errorf("gone: %+v", o)
|
||||
}
|
||||
|
||||
// A restart resumes the saved session: no new sign-in.
|
||||
signIns := api.SignIns
|
||||
h.mu.Lock()
|
||||
h.queue = []hub.Job{{ID: "list2", Kind: "list", IMVUCreator: 903}}
|
||||
h.mu.Unlock()
|
||||
again := newImporterIn(t, srv, cdn, dir)
|
||||
again.Key = im.Key
|
||||
again.APIURL = api.URL
|
||||
runUntil(t, again, func() bool { return h.settled(4) })
|
||||
if api.SignIns != signIns {
|
||||
t.Errorf("signed in again: %d sign-ins, was %d", api.SignIns, signIns)
|
||||
}
|
||||
// The refused account isn't tried again within the hour (same password), even after a restart.
|
||||
refusals := 0
|
||||
for _, r := range h.reportsOf["wrong"] {
|
||||
if r.State == "sign_in_failed" {
|
||||
refusals++
|
||||
}
|
||||
}
|
||||
if refusals != 1 {
|
||||
t.Errorf("the refused account was refused %d times", refusals)
|
||||
}
|
||||
|
||||
// IMVU ends the session: the importer signs in again.
|
||||
api.EndSessions()
|
||||
h.mu.Lock()
|
||||
h.queue = []hub.Job{{ID: "list3", Kind: "list", IMVUCreator: 903}, {ID: "list4", Kind: "list", IMVUCreator: 903}}
|
||||
h.mu.Unlock()
|
||||
runUntil(t, again, func() bool {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return (h.listings["list3"] != nil || h.reports["list3"].Status != "") && (h.listings["list4"] != nil || h.reports["list4"].Status != "")
|
||||
})
|
||||
if api.SignIns != signIns+1 || len(h.listings["list3"]) != 2 || len(h.listings["list4"]) != 2 {
|
||||
t.Errorf("after the session ended: %d sign-ins (want %d); listings %d and %d, reports %v", api.SignIns, signIns+1, len(h.listings["list3"]), len(h.listings["list4"]), h.reports)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user