2019-05-09 14:54:44 +02:00
|
|
|
package lbryinc
|
|
|
|
|
|
|
|
import (
|
2019-12-17 15:06:52 +01:00
|
|
|
"fmt"
|
2019-05-09 14:54:44 +02:00
|
|
|
"net/http"
|
2019-09-18 13:02:03 +02:00
|
|
|
"net/http/httptest"
|
2019-05-09 14:54:44 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-08-08 20:08:47 +02:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2019-05-09 14:54:44 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-02-16 13:40:18 +01:00
|
|
|
func launchDummyServer(lastReq **http.Request, path, response string, status int) *httptest.Server {
|
2019-09-18 13:02:03 +02:00
|
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-02-16 13:40:18 +01:00
|
|
|
if lastReq != nil {
|
|
|
|
*lastReq = &*r
|
|
|
|
}
|
2021-08-08 20:08:47 +02:00
|
|
|
authT := r.FormValue("auth_token")
|
|
|
|
if authT == "" {
|
|
|
|
accessT := r.Header.Get("Authorization")
|
|
|
|
if accessT == "" {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 15:06:52 +01:00
|
|
|
if r.URL.Path != path {
|
|
|
|
fmt.Printf("path doesn't match: %v != %v", r.URL.Path, path)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2021-02-16 13:40:18 +01:00
|
|
|
w.WriteHeader(status)
|
2019-12-17 15:06:52 +01:00
|
|
|
w.Write([]byte(response))
|
|
|
|
}
|
2019-09-18 13:02:03 +02:00
|
|
|
}))
|
2019-05-09 14:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserMe(t *testing.T) {
|
2021-02-16 13:40:18 +01:00
|
|
|
ts := launchDummyServer(nil, makeMethodPath(userObjectPath, userMeMethod), userMeResponse, http.StatusOK)
|
2019-09-18 13:02:03 +02:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
|
2019-05-09 14:54:44 +02:00
|
|
|
r, err := c.UserMe()
|
|
|
|
assert.Nil(t, err)
|
2019-12-17 15:06:52 +01:00
|
|
|
assert.Equal(t, "user@lbry.tv", r["primary_email"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserHasVerifiedEmail(t *testing.T) {
|
2021-02-16 13:40:18 +01:00
|
|
|
ts := launchDummyServer(nil, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), userHasVerifiedEmailResponse, http.StatusOK)
|
2019-12-17 15:06:52 +01:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
|
|
|
|
r, err := c.UserHasVerifiedEmail()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.EqualValues(t, 12345, r["user_id"])
|
|
|
|
assert.Equal(t, true, r["has_verified_email"])
|
2021-08-08 20:08:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserHasVerifiedEmailOAuth(t *testing.T) {
|
|
|
|
ts := launchDummyServer(nil, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), userHasVerifiedEmailResponse, http.StatusOK)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
c := NewOauthClient(oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "Test-Access-Token"}), &ClientOpts{ServerAddress: ts.URL})
|
|
|
|
r, err := c.UserHasVerifiedEmail()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.EqualValues(t, 12345, r["user_id"])
|
|
|
|
assert.Equal(t, true, r["has_verified_email"])
|
2019-05-09 14:54:44 +02:00
|
|
|
}
|
2019-09-18 13:02:03 +02:00
|
|
|
|
|
|
|
func TestRemoteIP(t *testing.T) {
|
|
|
|
var req *http.Request
|
2021-02-16 13:40:18 +01:00
|
|
|
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse, http.StatusOK)
|
2019-09-18 13:02:03 +02:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL, RemoteIP: "8.8.8.8"})
|
|
|
|
_, err := c.UserMe()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, []string{"8.8.8.8"}, req.Header["X-Forwarded-For"])
|
|
|
|
}
|
2019-12-17 15:06:52 +01:00
|
|
|
|
2021-02-16 12:30:09 +01:00
|
|
|
func TestWrongToken(t *testing.T) {
|
|
|
|
c := NewClient("zcasdasc", nil)
|
|
|
|
|
|
|
|
r, err := c.UserHasVerifiedEmail()
|
|
|
|
assert.Nil(t, r)
|
|
|
|
assert.EqualError(t, err, "api error: could not authenticate user")
|
|
|
|
assert.ErrorAs(t, err, &APIError{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPError(t *testing.T) {
|
|
|
|
c := NewClient("zcasdasc", &ClientOpts{ServerAddress: "http://lolcathost"})
|
|
|
|
|
|
|
|
r, err := c.UserHasVerifiedEmail()
|
|
|
|
assert.Nil(t, r)
|
|
|
|
assert.EqualError(t, err, `Post "http://lolcathost/user/has_verified_email": dial tcp: lookup lolcathost: no such host`)
|
|
|
|
}
|
|
|
|
|
2021-02-16 13:40:18 +01:00
|
|
|
func TestGatewayError(t *testing.T) {
|
|
|
|
var req *http.Request
|
|
|
|
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), "", http.StatusBadGateway)
|
|
|
|
defer ts.Close()
|
|
|
|
c := NewClient("zcasdasc", &ClientOpts{ServerAddress: ts.URL})
|
|
|
|
|
|
|
|
r, err := c.UserHasVerifiedEmail()
|
|
|
|
assert.Nil(t, r)
|
|
|
|
assert.EqualError(t, err, `server returned non-OK status: 502`)
|
|
|
|
}
|
|
|
|
|
2019-12-17 15:06:52 +01:00
|
|
|
const userMeResponse = `{
|
|
|
|
"success": true,
|
|
|
|
"error": null,
|
|
|
|
"data": {
|
|
|
|
"id": 12345,
|
|
|
|
"language": "en",
|
|
|
|
"given_name": null,
|
|
|
|
"family_name": null,
|
|
|
|
"created_at": "2019-01-17T12:13:06Z",
|
|
|
|
"updated_at": "2019-05-02T13:57:59Z",
|
|
|
|
"invited_by_id": null,
|
|
|
|
"invited_at": null,
|
|
|
|
"invites_remaining": 0,
|
|
|
|
"invite_reward_claimed": false,
|
|
|
|
"is_email_enabled": true,
|
|
|
|
"manual_approval_user_id": 654,
|
|
|
|
"reward_status_change_trigger": "manual",
|
|
|
|
"primary_email": "user@lbry.tv",
|
|
|
|
"has_verified_email": true,
|
|
|
|
"is_identity_verified": false,
|
|
|
|
"is_reward_approved": true,
|
|
|
|
"groups": []
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
const userHasVerifiedEmailResponse = `{
|
|
|
|
"success": true,
|
|
|
|
"error": null,
|
|
|
|
"data": {
|
|
|
|
"user_id": 12345,
|
|
|
|
"has_verified_email": true
|
|
|
|
}
|
|
|
|
}`
|