|
| 1 | +package threads |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func TestExchangeCodeForToken_Success(t *testing.T) { |
| 13 | + handler := func(w http.ResponseWriter, r *http.Request) { |
| 14 | + if r.Method != "POST" { |
| 15 | + http.NotFound(w, r) |
| 16 | + return |
| 17 | + } |
| 18 | + w.Header().Set("Content-Type", "application/json") |
| 19 | + w.WriteHeader(200) |
| 20 | + _, _ = w.Write([]byte(`{ |
| 21 | + "access_token": "new_token_123", |
| 22 | + "token_type": "bearer", |
| 23 | + "expires_in": 3600, |
| 24 | + "user_id": 99999 |
| 25 | + }`)) |
| 26 | + } |
| 27 | + |
| 28 | + server := httptest.NewServer(http.HandlerFunc(handler)) |
| 29 | + t.Cleanup(server.Close) |
| 30 | + |
| 31 | + config := &Config{ |
| 32 | + ClientID: "test-id", |
| 33 | + ClientSecret: "test-secret", |
| 34 | + RedirectURI: "https://example.com/callback", |
| 35 | + } |
| 36 | + config.SetDefaults() |
| 37 | + config.BaseURL = server.URL |
| 38 | + |
| 39 | + client, err := NewClient(config) |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + |
| 44 | + err = client.ExchangeCodeForToken(context.Background(), "auth_code_123") |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("unexpected error: %v", err) |
| 47 | + } |
| 48 | + if !client.IsAuthenticated() { |
| 49 | + t.Error("expected client to be authenticated") |
| 50 | + } |
| 51 | + tokenInfo := client.GetTokenInfo() |
| 52 | + if tokenInfo.AccessToken != "new_token_123" { |
| 53 | + t.Errorf("expected new_token_123, got %s", tokenInfo.AccessToken) |
| 54 | + } |
| 55 | + if tokenInfo.UserID != "99999" { |
| 56 | + t.Errorf("expected user ID 99999, got %s", tokenInfo.UserID) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestExchangeCodeForToken_EmptyCode(t *testing.T) { |
| 61 | + config := &Config{ |
| 62 | + ClientID: "test-id", |
| 63 | + ClientSecret: "test-secret", |
| 64 | + RedirectURI: "https://example.com/callback", |
| 65 | + } |
| 66 | + config.SetDefaults() |
| 67 | + client, _ := NewClient(config) |
| 68 | + |
| 69 | + err := client.ExchangeCodeForToken(context.Background(), "") |
| 70 | + if err == nil { |
| 71 | + t.Fatal("expected error for empty code") |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestGetLongLivedToken_Success(t *testing.T) { |
| 76 | + client := testClient(t, jsonHandler(200, `{ |
| 77 | + "access_token": "long_lived_token", |
| 78 | + "token_type": "bearer", |
| 79 | + "expires_in": 5184000 |
| 80 | + }`)) |
| 81 | + |
| 82 | + err := client.GetLongLivedToken(context.Background()) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("unexpected error: %v", err) |
| 85 | + } |
| 86 | + tokenInfo := client.GetTokenInfo() |
| 87 | + if tokenInfo.AccessToken != "long_lived_token" { |
| 88 | + t.Errorf("expected long_lived_token, got %s", tokenInfo.AccessToken) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestRefreshToken_Success(t *testing.T) { |
| 93 | + client := testClient(t, jsonHandler(200, `{ |
| 94 | + "access_token": "refreshed_token", |
| 95 | + "token_type": "bearer", |
| 96 | + "expires_in": 5184000 |
| 97 | + }`)) |
| 98 | + |
| 99 | + err := client.RefreshToken(context.Background()) |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("unexpected error: %v", err) |
| 102 | + } |
| 103 | + tokenInfo := client.GetTokenInfo() |
| 104 | + if tokenInfo.AccessToken != "refreshed_token" { |
| 105 | + t.Errorf("expected refreshed_token, got %s", tokenInfo.AccessToken) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestRefreshToken_NoToken(t *testing.T) { |
| 110 | + config := &Config{ |
| 111 | + ClientID: "test-id", |
| 112 | + ClientSecret: "test-secret", |
| 113 | + RedirectURI: "https://example.com/callback", |
| 114 | + } |
| 115 | + config.SetDefaults() |
| 116 | + client, _ := NewClient(config) |
| 117 | + |
| 118 | + err := client.RefreshToken(context.Background()) |
| 119 | + if err == nil { |
| 120 | + t.Fatal("expected error when no token") |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestDebugToken_Success(t *testing.T) { |
| 125 | + client := testClient(t, jsonHandler(200, `{ |
| 126 | + "data": { |
| 127 | + "type": "USER", |
| 128 | + "application": "Test App", |
| 129 | + "is_valid": true, |
| 130 | + "expires_at": 1735689600, |
| 131 | + "issued_at": 1735603200, |
| 132 | + "user_id": "12345", |
| 133 | + "scopes": ["threads_basic"] |
| 134 | + } |
| 135 | + }`)) |
| 136 | + |
| 137 | + resp, err := client.DebugToken(context.Background(), "test-token") |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("unexpected error: %v", err) |
| 140 | + } |
| 141 | + if !resp.Data.IsValid { |
| 142 | + t.Error("expected valid token") |
| 143 | + } |
| 144 | + if resp.Data.UserID != "12345" { |
| 145 | + t.Errorf("expected user ID 12345, got %s", resp.Data.UserID) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestGetAuthURL_ContainsRequiredParams(t *testing.T) { |
| 150 | + config := &Config{ |
| 151 | + ClientID: "my-app-id", |
| 152 | + ClientSecret: "secret", |
| 153 | + RedirectURI: "https://example.com/callback", |
| 154 | + } |
| 155 | + config.SetDefaults() |
| 156 | + client, _ := NewClient(config) |
| 157 | + |
| 158 | + authURL := client.GetAuthURL([]string{"threads_basic"}) |
| 159 | + if authURL == "" { |
| 160 | + t.Fatal("expected non-empty auth URL") |
| 161 | + } |
| 162 | + for _, param := range []string{"client_id=my-app-id", "response_type=code", "scope=threads_basic"} { |
| 163 | + if !strings.Contains(authURL, param) { |
| 164 | + t.Errorf("expected auth URL to contain %q, got %s", param, authURL) |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestTokenExpiration(t *testing.T) { |
| 170 | + config := &Config{ |
| 171 | + ClientID: "test-id", |
| 172 | + ClientSecret: "test-secret", |
| 173 | + RedirectURI: "https://example.com/callback", |
| 174 | + } |
| 175 | + config.SetDefaults() |
| 176 | + client, _ := NewClient(config) |
| 177 | + |
| 178 | + _ = client.SetTokenInfo(&TokenInfo{ |
| 179 | + AccessToken: "expired", |
| 180 | + TokenType: "Bearer", |
| 181 | + ExpiresAt: time.Now().Add(-time.Hour), |
| 182 | + UserID: "12345", |
| 183 | + CreatedAt: time.Now().Add(-2 * time.Hour), |
| 184 | + }) |
| 185 | + |
| 186 | + if !client.IsTokenExpired() { |
| 187 | + t.Error("expected token to be expired") |
| 188 | + } |
| 189 | + if !client.IsTokenExpiringSoon(time.Hour) { |
| 190 | + t.Error("expected token to be expiring soon") |
| 191 | + } |
| 192 | +} |
0 commit comments