]> git.giacomolli.uk - onechat/blobdiff - chatgpt_fake_helper_test.go
Check the complete message before committing
[onechat] / chatgpt_fake_helper_test.go
index 0dca6bb7f5c67872eb0106d7f487bae7f94e058a..a25ed1294e22fad0414928446cff0dafef2a9908 100644 (file)
@@ -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)
+}