X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/f5c601f1801aaa276f8c335fcb51c373edc0b50c..f4509310b5abe5012cb83bed3960404e8fa92217:/chatgpt_fake_helper_test.go?ds=inline diff --git a/chatgpt_fake_helper_test.go b/chatgpt_fake_helper_test.go index 0dca6bb..a25ed12 100644 --- a/chatgpt_fake_helper_test.go +++ b/chatgpt_fake_helper_test.go @@ -56,36 +56,6 @@ func newTestChatGPTAuth(t *testing.T, server *ChatGPTFake) *ChatGPTAuth { return newChatGPTAuth(filepath.Join(t.TempDir(), chatgptTokenFileName), server.URL) } -// 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) -} - -// 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) - } - - encode := base64.RawURLEncoding.EncodeToString - token := encode([]byte(`{"alg":"none"}`)) + "." + encode(claims) + "." + encode([]byte("fake")) - f.access = append(f.access, token) - - return token -} - func (f *ChatGPTFake) userCode(w http.ResponseWriter, r *http.Request) { var body map[string]string @@ -187,16 +157,34 @@ func (f *ChatGPTFake) oauthToken(w http.ResponseWriter, r *http.Request) { chatgptWriteJSON(w, answer) } -func chatgptWriteTestTokens(t *testing.T, auth *ChatGPTAuth, access, refresh string) { - t.Helper() - - err := auth.write(ChatGPTTokenFile{ - AccessToken: access, - RefreshToken: refresh, - IDToken: access, - AccountID: chatgptTestAccount, +// 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), }) - require.NoError(t, err) + if err != nil { + panic(err) + } + + encode := base64.RawURLEncoding.EncodeToString + token := encode([]byte(`{"alg":"none"}`)) + "." + encode(claims) + "." + encode([]byte("fake")) + f.access = append(f.access, token) + + return token +} + +// 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 chatgptWriteJSON(w http.ResponseWriter, value any) { @@ -207,3 +195,15 @@ func chatgptWriteJSON(w http.ResponseWriter, value any) { panic(fmt.Sprint("encode answer: ", err)) } } + +func chatgptWriteTestTokens(t *testing.T, auth *ChatGPTAuth, access, refresh string) { + t.Helper() + + err := auth.write(ChatGPTTokenFile{ + AccessToken: access, + RefreshToken: refresh, + IDToken: access, + AccountID: chatgptTestAccount, + }) + require.NoError(t, err) +}