package memoriatest
import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
)
// nativeApprovalPage starts a native sign-in on h and opens its approval
// page as c, the signed-in browser, returning the response.
func nativeApprovalPage(t *testing.T, h http.Handler, c *client) *httptest.ResponseRecorder {
t.Helper()
sum := sha256.Sum256([]byte(strings.Repeat("v", 43)))
req := httptest.NewRequest(http.MethodPost, testOrigin+"/native/request",
strings.NewReader(`{"challenge":"`+base64.RawURLEncoding.EncodeToString(sum[:])+`"}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Origin", testOrigin)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
var out struct{ ID string }
if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &out) != nil || out.ID == "" {
t.Fatalf("POST /native/request = %d %q", rec.Code, rec.Body.String())
}
page := c.get(testOrigin + "/native/authorize?id=" + out.ID)
if page.Code != http.StatusOK || !strings.Contains(page.Body.String(), "Sign in to Memoria app") {
t.Fatalf("GET /native/authorize = %d, not the approval page:\n%s", page.Code, page.Body.String())
}
return page
}
var styleElement = regexp.MustCompile(`(?s)`)
// Astra on #27, P2: the native kit's approval page (/native/authorize) is
// styled by one inline