X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/522d7fd117285e4e4ce3ea5ec9b5a1722521b681..9346d20fe4297dce1aa16171749a7ef3e41ce7bd:/chatgpt_fake_helper_test.go?ds=inline diff --git a/chatgpt_fake_helper_test.go b/chatgpt_fake_helper_test.go index ddd4c67..a25ed12 100644 --- a/chatgpt_fake_helper_test.go +++ b/chatgpt_fake_helper_test.go @@ -35,34 +35,25 @@ type ChatGPTFake struct { access []string } -// token returns a new access token that expires after ttl, for a token file -// that a test writes. -func (f *ChatGPTFake) token(ttl time.Duration) string { - f.mutex.Lock() - defer f.mutex.Unlock() +func newChatGPTFake(t *testing.T) *ChatGPTFake { + t.Helper() - return f.newToken(ttl) -} + f := &ChatGPTFake{} + mux := http.NewServeMux() + mux.HandleFunc("POST /api/accounts/deviceauth/usercode", f.userCode) + mux.HandleFunc("POST /api/accounts/deviceauth/token", f.deviceToken) + mux.HandleFunc("POST /oauth/token", f.oauthToken) -// newToken returns a new fake JWT with the expiry and the account claim, and -// keeps it in access. The caller holds the mutex. -func (f *ChatGPTFake) newToken(ttl time.Duration) string { - claims, err := json.Marshal(map[string]any{ - "exp": time.Now().Add(ttl).Unix(), - chatgptAuthClaim: map[string]any{ - "chatgpt_account_id": chatgptTestAccount, - }, - "serial": len(f.access), - }) - if err != nil { - panic(err) - } + f.Server = httptest.NewServer(mux) + t.Cleanup(f.Close) - encode := base64.RawURLEncoding.EncodeToString - token := encode([]byte(`{"alg":"none"}`)) + "." + encode(claims) + "." + encode([]byte("fake")) - f.access = append(f.access, token) + return f +} - return token +func newTestChatGPTAuth(t *testing.T, server *ChatGPTFake) *ChatGPTAuth { + t.Helper() + + return newChatGPTAuth(filepath.Join(t.TempDir(), chatgptTokenFileName), server.URL) } func (f *ChatGPTFake) userCode(w http.ResponseWriter, r *http.Request) { @@ -166,25 +157,43 @@ func (f *ChatGPTFake) oauthToken(w http.ResponseWriter, r *http.Request) { chatgptWriteJSON(w, answer) } -func newChatGPTFake(t *testing.T) *ChatGPTFake { - t.Helper() +// newToken returns a new fake JWT with the expiry and the account claim, and +// keeps it in access. The caller holds the mutex. +func (f *ChatGPTFake) newToken(ttl time.Duration) string { + claims, err := json.Marshal(map[string]any{ + "exp": time.Now().Add(ttl).Unix(), + chatgptAuthClaim: map[string]any{ + "chatgpt_account_id": chatgptTestAccount, + }, + "serial": len(f.access), + }) + if err != nil { + panic(err) + } - f := &ChatGPTFake{} - mux := http.NewServeMux() - mux.HandleFunc("POST /api/accounts/deviceauth/usercode", f.userCode) - mux.HandleFunc("POST /api/accounts/deviceauth/token", f.deviceToken) - mux.HandleFunc("POST /oauth/token", f.oauthToken) + encode := base64.RawURLEncoding.EncodeToString + token := encode([]byte(`{"alg":"none"}`)) + "." + encode(claims) + "." + encode([]byte("fake")) + f.access = append(f.access, token) - f.Server = httptest.NewServer(mux) - t.Cleanup(f.Close) + return token +} - return f +// token returns a new access token that expires after ttl, for a token file +// that a test writes. +func (f *ChatGPTFake) token(ttl time.Duration) string { + f.mutex.Lock() + defer f.mutex.Unlock() + + return f.newToken(ttl) } -func newTestChatGPTAuth(t *testing.T, server *ChatGPTFake) *ChatGPTAuth { - t.Helper() +func chatgptWriteJSON(w http.ResponseWriter, value any) { + w.Header().Set("Content-Type", "application/json") - return newChatGPTAuth(filepath.Join(t.TempDir(), chatgptTokenFileName), server.URL) + err := json.NewEncoder(w).Encode(value) + if err != nil { + panic(fmt.Sprint("encode answer: ", err)) + } } func chatgptWriteTestTokens(t *testing.T, auth *ChatGPTAuth, access, refresh string) { @@ -198,12 +207,3 @@ func chatgptWriteTestTokens(t *testing.T, auth *ChatGPTAuth, access, refresh str }) require.NoError(t, err) } - -func chatgptWriteJSON(w http.ResponseWriter, value any) { - w.Header().Set("Content-Type", "application/json") - - err := json.NewEncoder(w).Encode(value) - if err != nil { - panic(fmt.Sprint("encode answer: ", err)) - } -}