Add has_verified_email method to lbryinc client #78

Merged
anbsky merged 1 commit from feature/lbryinc_has_verified_email into master 2019-12-18 09:18:11 +01:00
4 changed files with 81 additions and 35 deletions
Showing only changes of commit d12e431b40 - Show all commits

View file

@ -20,6 +20,7 @@ const (
userObjectPath = "user" userObjectPath = "user"
userMeMethod = "me" userMeMethod = "me"
userHasVerifiedEmailMethod = "has_verified_email"
) )
// Client stores data about internal-apis call it is about to make. // Client stores data about internal-apis call it is about to make.
@ -48,6 +49,10 @@ type APIResponse struct {
// ResponseData is a map containing parsed json response. // ResponseData is a map containing parsed json response.
type ResponseData map[string]interface{} type ResponseData map[string]interface{}
func makeMethodPath(obj, method string) string {
return fmt.Sprintf("/%s/%s", obj, method)
}
// NewClient returns a client instance for internal-apis. It requires authToken to be provided // NewClient returns a client instance for internal-apis. It requires authToken to be provided
// for authentication. // for authentication.
func NewClient(authToken string, opts *ClientOpts) Client { func NewClient(authToken string, opts *ClientOpts) Client {
@ -70,7 +75,7 @@ func NewClient(authToken string, opts *ClientOpts) Client {
} }
func (c Client) getEndpointURL(object, method string) string { func (c Client) getEndpointURL(object, method string) string {
return fmt.Sprintf("%s/%s/%s", c.serverAddress, object, method) return fmt.Sprintf("%s%s", c.serverAddress, makeMethodPath(object, method))
} }
func (c Client) prepareParams(params map[string]interface{}) (string, error) { func (c Client) prepareParams(params map[string]interface{}) (string, error) {
@ -137,3 +142,7 @@ func (c Client) Call(object, method string, params map[string]interface{}) (Resp
func (c Client) UserMe() (ResponseData, error) { func (c Client) UserMe() (ResponseData, error) {
return c.Call(userObjectPath, userMeMethod, map[string]interface{}{}) return c.Call(userObjectPath, userMeMethod, map[string]interface{}{})
} }
func (c Client) UserHasVerifiedEmail() (ResponseData, error) {
return c.Call(userObjectPath, userHasVerifiedEmailMethod, map[string]interface{}{})
}

View file

@ -1,6 +1,7 @@
package lbryinc package lbryinc
import ( import (
"fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -17,16 +18,67 @@ func TestUserMeWrongToken(t *testing.T) {
assert.Nil(t, r) assert.Nil(t, r)
} }
func launchDummyServer(lastReq **http.Request) *httptest.Server { func TestUserHasVerifiedEmailWrongToken(t *testing.T) {
c := NewClient("abc", nil)
r, err := c.UserHasVerifiedEmail()
require.NotNil(t, err)
assert.Equal(t, "could not authenticate user", err.Error())
assert.Nil(t, r)
}
func launchDummyServer(lastReq **http.Request, path, response string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
*lastReq = &*r *lastReq = &*r
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") w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
response := []byte(`{ w.Write([]byte(response))
}
}))
}
func TestUserMe(t *testing.T) {
var req *http.Request
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse)
defer ts.Close()
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
r, err := c.UserMe()
assert.Nil(t, err)
assert.Equal(t, "user@lbry.tv", r["primary_email"])
}
func TestUserHasVerifiedEmail(t *testing.T) {
var req *http.Request
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), userHasVerifiedEmailResponse)
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"])
}
func TestRemoteIP(t *testing.T) {
var req *http.Request
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse)
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"])
}
const userMeResponse = `{
"success": true, "success": true,
"error": null, "error": null,
"data": { "data": {
"id": 751365, "id": 12345,
"language": "en", "language": "en",
"given_name": null, "given_name": null,
"family_name": null, "family_name": null,
@ -37,37 +89,21 @@ func launchDummyServer(lastReq **http.Request) *httptest.Server {
"invites_remaining": 0, "invites_remaining": 0,
"invite_reward_claimed": false, "invite_reward_claimed": false,
"is_email_enabled": true, "is_email_enabled": true,
"manual_approval_user_id": 837139, "manual_approval_user_id": 654,
"reward_status_change_trigger": "manual", "reward_status_change_trigger": "manual",
"primary_email": "andrey@lbry.com", "primary_email": "user@lbry.tv",
"has_verified_email": true, "has_verified_email": true,
"is_identity_verified": false, "is_identity_verified": false,
"is_reward_approved": true, "is_reward_approved": true,
"groups": [] "groups": []
} }
}`) }`
w.Write(response)
}))
}
func TestUserMe(t *testing.T) { const userHasVerifiedEmailResponse = `{
var req *http.Request "success": true,
ts := launchDummyServer(&req) "error": null,
defer ts.Close() "data": {
"user_id": 12345,
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL}) "has_verified_email": true
r, err := c.UserMe() }
assert.Nil(t, err) }`
assert.Equal(t, r["primary_email"], "andrey@lbry.com")
}
func TestRemoteIP(t *testing.T) {
var req *http.Request
ts := launchDummyServer(&req)
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"])
}

2
go.mod
View file

@ -17,7 +17,7 @@ require (
github.com/gorilla/websocket v1.4.1 // indirect github.com/gorilla/websocket v1.4.1 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pretty v0.1.0 // indirect github.com/kr/pretty v0.1.0 // indirect
github.com/lbryio/lbry.go v1.1.2 // indirect github.com/lbryio/lbry.go v1.1.2
github.com/lbryio/lbryschema.go v0.0.0-20190602173230-6d2f69a36f46 github.com/lbryio/lbryschema.go v0.0.0-20190602173230-6d2f69a36f46
github.com/lbryio/ozzo-validation v0.0.0-20170323141101-d1008ad1fd04 github.com/lbryio/ozzo-validation v0.0.0-20170323141101-d1008ad1fd04
github.com/lbryio/types v0.0.0-20191009145016-1bb8107e04f8 github.com/lbryio/types v0.0.0-20191009145016-1bb8107e04f8

1
go.sum
View file

@ -193,6 +193,7 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=