11package lbryinc
22
33import (
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
5343func 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
6466func 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+ }`
0 commit comments