-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
545 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package twitter | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
"github.com/go-chi/chi/v5" | ||
) | ||
|
||
var ( | ||
users = map[string]*TwitterUserApiResponse{} | ||
tweets = map[string]*TweetApiResponse{} | ||
) | ||
|
||
func init() { | ||
users["pajlada"] = &TwitterUserApiResponse{ | ||
Name: "PAJLADA", | ||
Username: "pajlada", | ||
Description: "Cool memer", | ||
Followers: 69, | ||
ProfileImageUrl: "https://pbs.twimg.com/profile_images/1385924241619628033/fW7givJA_400x400.jpg", | ||
} | ||
|
||
// Tweet with no entities | ||
tweets["1507648130682077194"] = &TweetApiResponse{ | ||
Text: "Digging a hole", | ||
User: APIUser{ | ||
Name: "PAJLADA", | ||
Username: "pajlada", | ||
}, | ||
Likes: 69, | ||
Retweets: 420, | ||
Timestamp: "Sat Mar 26 17:15:50 +0200 2022", | ||
} | ||
|
||
// Tweet with entities | ||
tweets["1506968434134953986"] = &TweetApiResponse{ | ||
Text: "", | ||
User: APIUser{ | ||
Name: "PAJLADA", | ||
Username: "pajlada", | ||
}, | ||
Likes: 69, | ||
Retweets: 420, | ||
Timestamp: "Sat Mar 26 17:15:50 +0200 2022", | ||
Entities: APIEntities{ | ||
Media: []APIEntitiesMedia{ | ||
{ | ||
Url: "https://pbs.twimg.com/media/FOnTzeQWUAMU6L1?format=jpg&name=medium", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// Tweet with poorly formatted timestamp | ||
tweets["1505121705290874881"] = &TweetApiResponse{ | ||
Text: "Bad timestamp", | ||
User: APIUser{ | ||
Name: "PAJLADA", | ||
Username: "pajlada", | ||
}, | ||
Likes: 420, | ||
Retweets: 69, | ||
Timestamp: "asdasd", | ||
} | ||
} | ||
|
||
func testServer() *httptest.Server { | ||
r := chi.NewRouter() | ||
r.Get("/1.1/users/show.json", func(w http.ResponseWriter, r *http.Request) { | ||
screenName := r.URL.Query().Get("screen_name") | ||
|
||
var response *TwitterUserApiResponse | ||
var ok bool | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
|
||
if screenName == "bad" { | ||
w.Write([]byte("xD")) | ||
} else if screenName == "500" { | ||
http.Error(w, http.StatusText(500), 500) | ||
return | ||
} else if response, ok = users[screenName]; !ok { | ||
http.Error(w, http.StatusText(404), 404) | ||
return | ||
} | ||
|
||
b, _ := json.Marshal(&response) | ||
|
||
w.Write(b) | ||
}) | ||
r.Get("/1.1/statuses/show.json", func(w http.ResponseWriter, r *http.Request) { | ||
tweetID := r.URL.Query().Get("id") | ||
|
||
var response *TweetApiResponse | ||
var ok bool | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
|
||
if tweetID == "bad" { | ||
w.Write([]byte("xD")) | ||
} else if tweetID == "500" { | ||
http.Error(w, http.StatusText(500), 500) | ||
return | ||
} else if response, ok = tweets[tweetID]; !ok { | ||
http.Error(w, http.StatusText(404), 404) | ||
return | ||
} | ||
|
||
b, _ := json.Marshal(&response) | ||
|
||
w.Write(b) | ||
}) | ||
return httptest.NewServer(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package twitter | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/Chatterino/api/internal/logger" | ||
"github.com/Chatterino/api/pkg/config" | ||
"github.com/Chatterino/api/pkg/resolver" | ||
"github.com/pashagolub/pgxmock" | ||
|
||
qt "github.com/frankban/quicktest" | ||
) | ||
|
||
func TestInitialize(t *testing.T) { | ||
ctx := logger.OnContext(context.Background(), logger.NewTest()) | ||
c := qt.New(t) | ||
|
||
pool, err := pgxmock.NewPool() | ||
c.Assert(err, qt.IsNil) | ||
|
||
c.Run("No credentials", func(c *qt.C) { | ||
cfg := config.APIConfig{} | ||
customResolvers := []resolver.Resolver{} | ||
c.Assert(customResolvers, qt.HasLen, 0) | ||
Initialize(ctx, cfg, pool, &customResolvers) | ||
c.Assert(customResolvers, qt.HasLen, 0) | ||
}) | ||
|
||
c.Run("Credentials", func(c *qt.C) { | ||
cfg := config.APIConfig{ | ||
TwitterBearerToken: "test", | ||
} | ||
customResolvers := []resolver.Resolver{} | ||
c.Assert(customResolvers, qt.HasLen, 0) | ||
Initialize(ctx, cfg, pool, &customResolvers) | ||
c.Assert(customResolvers, qt.HasLen, 1) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.