43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/imroc/req/v3"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestFetchChatGPTSubscriptionExpiresAt(t *testing.T) {
|
|
const wantExpiresAt = "2026-06-10T02:52:15Z"
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
require.Equal(t, "/backend-api/subscriptions", r.URL.Path)
|
|
require.Equal(t, "acc_123", r.URL.Query().Get("account_id"))
|
|
require.Equal(t, "Bearer access-token", r.Header.Get("Authorization"))
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"plan_type": "plus",
|
|
"active_until": wantExpiresAt,
|
|
"will_renew": true,
|
|
"id": "sub_123",
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
oldURL := chatGPTSubscriptionsURL
|
|
chatGPTSubscriptionsURL = server.URL + "/backend-api/subscriptions"
|
|
t.Cleanup(func() { chatGPTSubscriptionsURL = oldURL })
|
|
|
|
got := fetchChatGPTSubscriptionExpiresAt(context.Background(), func(proxyURL string) (*req.Client, error) {
|
|
return req.C().SetTimeout(5 * time.Second), nil
|
|
}, "access-token", "", "acc_123")
|
|
|
|
require.Equal(t, wantExpiresAt, got)
|
|
}
|