Skip to content

Commit d12e431

Browse files
committed
Add has_verified_email method to lbryinc client
1 parent 8f8b2e6 commit d12e431

File tree

4 files changed

+81
-35
lines changed

4 files changed

+81
-35
lines changed

extras/lbryinc/client.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const (
1818
timeout = 5 * time.Second
1919
headerForwardedFor = "X-Forwarded-For"
2020

21-
userObjectPath = "user"
22-
userMeMethod = "me"
21+
userObjectPath = "user"
22+
userMeMethod = "me"
23+
userHasVerifiedEmailMethod = "has_verified_email"
2324
)
2425

2526
// Client stores data about internal-apis call it is about to make.
@@ -48,6 +49,10 @@ type APIResponse struct {
4849
// ResponseData is a map containing parsed json response.
4950
type ResponseData map[string]interface{}
5051

52+
func makeMethodPath(obj, method string) string {
53+
return fmt.Sprintf("/%s/%s", obj, method)
54+
}
55+
5156
// NewClient returns a client instance for internal-apis. It requires authToken to be provided
5257
// for authentication.
5358
func NewClient(authToken string, opts *ClientOpts) Client {
@@ -70,7 +75,7 @@ func NewClient(authToken string, opts *ClientOpts) Client {
7075
}
7176

7277
func (c Client) getEndpointURL(object, method string) string {
73-
return fmt.Sprintf("%s/%s/%s", c.serverAddress, object, method)
78+
return fmt.Sprintf("%s%s", c.serverAddress, makeMethodPath(object, method))
7479
}
7580

7681
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
137142
func (c Client) UserMe() (ResponseData, error) {
138143
return c.Call(userObjectPath, userMeMethod, map[string]interface{}{})
139144
}
145+
146+
func (c Client) UserHasVerifiedEmail() (ResponseData, error) {
147+
return c.Call(userObjectPath, userHasVerifiedEmailMethod, map[string]interface{}{})
148+
}

extras/lbryinc/client_test.go

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lbryinc
22

33
import (
4+
"fmt"
45
"net/http"
56
"net/http/httptest"
67
"testing"
@@ -17,57 +18,92 @@ func TestUserMeWrongToken(t *testing.T) {
1718
assert.Nil(t, r)
1819
}
1920

20-
func launchDummyServer(lastReq **http.Request) *httptest.Server {
21+
func TestUserHasVerifiedEmailWrongToken(t *testing.T) {
22+
c := NewClient("abc", nil)
23+
r, err := c.UserHasVerifiedEmail()
24+
require.NotNil(t, err)
25+
assert.Equal(t, "could not authenticate user", err.Error())
26+
assert.Nil(t, r)
27+
}
28+
29+
func launchDummyServer(lastReq **http.Request, path, response string) *httptest.Server {
2130
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2231
*lastReq = &*r
23-
w.Header().Set("Content-Type", "application/json; charset=utf-8")
24-
w.WriteHeader(http.StatusOK)
25-
response := []byte(`{
26-
"success": true,
27-
"error": null,
28-
"data": {
29-
"id": 751365,
30-
"language": "en",
31-
"given_name": null,
32-
"family_name": null,
33-
"created_at": "2019-01-17T12:13:06Z",
34-
"updated_at": "2019-05-02T13:57:59Z",
35-
"invited_by_id": null,
36-
"invited_at": null,
37-
"invites_remaining": 0,
38-
"invite_reward_claimed": false,
39-
"is_email_enabled": true,
40-
"manual_approval_user_id": 837139,
41-
"reward_status_change_trigger": "manual",
42-
"primary_email": "[email protected]",
43-
"has_verified_email": true,
44-
"is_identity_verified": false,
45-
"is_reward_approved": true,
46-
"groups": []
47-
}
48-
}`)
49-
w.Write(response)
32+
if r.URL.Path != path {
33+
fmt.Printf("path doesn't match: %v != %v", r.URL.Path, path)
34+
w.WriteHeader(http.StatusNotFound)
35+
} else {
36+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
37+
w.WriteHeader(http.StatusOK)
38+
w.Write([]byte(response))
39+
}
5040
}))
5141
}
5242

5343
func TestUserMe(t *testing.T) {
5444
var req *http.Request
55-
ts := launchDummyServer(&req)
45+
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse)
5646
defer ts.Close()
5747

5848
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
5949
r, err := c.UserMe()
6050
assert.Nil(t, err)
61-
assert.Equal(t, r["primary_email"], "[email protected]")
51+
assert.Equal(t, "[email protected]", r["primary_email"])
52+
}
53+
54+
func TestUserHasVerifiedEmail(t *testing.T) {
55+
var req *http.Request
56+
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), userHasVerifiedEmailResponse)
57+
defer ts.Close()
58+
59+
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
60+
r, err := c.UserHasVerifiedEmail()
61+
assert.Nil(t, err)
62+
assert.EqualValues(t, 12345, r["user_id"])
63+
assert.Equal(t, true, r["has_verified_email"])
6264
}
6365

6466
func TestRemoteIP(t *testing.T) {
6567
var req *http.Request
66-
ts := launchDummyServer(&req)
68+
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse)
6769
defer ts.Close()
6870

6971
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL, RemoteIP: "8.8.8.8"})
7072
_, err := c.UserMe()
7173
assert.Nil(t, err)
7274
assert.Equal(t, []string{"8.8.8.8"}, req.Header["X-Forwarded-For"])
7375
}
76+
77+
const userMeResponse = `{
78+
"success": true,
79+
"error": null,
80+
"data": {
81+
"id": 12345,
82+
"language": "en",
83+
"given_name": null,
84+
"family_name": null,
85+
"created_at": "2019-01-17T12:13:06Z",
86+
"updated_at": "2019-05-02T13:57:59Z",
87+
"invited_by_id": null,
88+
"invited_at": null,
89+
"invites_remaining": 0,
90+
"invite_reward_claimed": false,
91+
"is_email_enabled": true,
92+
"manual_approval_user_id": 654,
93+
"reward_status_change_trigger": "manual",
94+
"primary_email": "[email protected]",
95+
"has_verified_email": true,
96+
"is_identity_verified": false,
97+
"is_reward_approved": true,
98+
"groups": []
99+
}
100+
}`
101+
102+
const userHasVerifiedEmailResponse = `{
103+
"success": true,
104+
"error": null,
105+
"data": {
106+
"user_id": 12345,
107+
"has_verified_email": true
108+
}
109+
}`

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/gorilla/websocket v1.4.1 // indirect
1818
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1919
github.com/kr/pretty v0.1.0 // indirect
20-
github.com/lbryio/lbry.go v1.1.2 // indirect
20+
github.com/lbryio/lbry.go v1.1.2
2121
github.com/lbryio/lbryschema.go v0.0.0-20190602173230-6d2f69a36f46
2222
github.com/lbryio/ozzo-validation v0.0.0-20170323141101-d1008ad1fd04
2323
github.com/lbryio/types v0.0.0-20191009145016-1bb8107e04f8

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3
193193
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
194194
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
195195
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
196+
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
196197
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
197198
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
198199
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=

0 commit comments

Comments
 (0)