Skip to content

Commit

Permalink
services: Verify (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel authored Dec 21, 2020
1 parent 0512e16 commit 926fad6
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 46 deletions.
7 changes: 3 additions & 4 deletions user/services/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ func TestGithub(t *testing.T) {

usr, err := user.New(kid, "github", "gabriel", urs, 1)
require.NoError(t, err)
st, msg, err := services.RequestVerify(context.TODO(), services.Github, client, usr)
require.NoError(t, err)
require.Equal(t, user.StatusOK, st)
result := services.Verify(context.TODO(), services.Github, client, usr)
require.Equal(t, user.StatusOK, result.Status)
expected := `BEGIN MESSAGE.
kdZaJI1U5AS7G6i VoUxdP8OtPzEoM6 pYhVl0YQZJnotVE wLg9BDb5SUO05pm
abUSeCvBfdPoRpP J8wrcF5PP3wTCKq 6Xr2MZHgg6m2Qal gJCD6vMqlBQfIg6
QsfB27aP5DMuXlJ AUVIAvMDHIoptmS riNMzfpwBjRShVL WH70a0GOEqD6L8b
kC5EFOwCedvHFpc AQVqULHjcSpeCfZ EIOaQ2IP.
END MESSAGE.`
require.Equal(t, expected, msg)
require.Equal(t, expected, result.Statement)
}
7 changes: 3 additions & 4 deletions user/services/https_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ func TestHTTPS(t *testing.T) {
usr, err := user.New(kid, "https", "keys.pub", "https://keys.pub/keyspub.txt", 1)
require.NoError(t, err)
client := http.NewClient()
st, msg, err := services.RequestVerify(context.TODO(), services.HTTPS, client, usr)
require.NoError(t, err)
require.Equal(t, user.StatusOK, st)
result := services.Verify(context.TODO(), services.HTTPS, client, usr)
require.Equal(t, user.StatusOK, result.Status)
expected := `BEGIN MESSAGE.
7PPiOMcdjhvnXzM 1uVwr224ccgiOKt I5vwzYoRY3xgUdL 86O3X1DnuZwCTIP
ACnuZKXBB4y39qQ f7sq7eoQs8oTCKq 6Xr2MZHgg7F8Mca NbI7en6mNzlIVvQ
zIh84hprPPEByeP D9s1xc5HURCNFcv rsOvrUoV0oHQfyi 89aehuNSV2AP9hp
8dGT8SwS3TEo3FP b1X8S32XyBenWKF aJv7L2IP.
END MESSAGE.`
require.Equal(t, expected, msg)
require.Equal(t, expected, result.Statement)
}
7 changes: 3 additions & 4 deletions user/services/keyspub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ func TestKeysPub(t *testing.T) {

client := http.NewClient()

st, msg, err := services.RequestVerify(context.TODO(), services.KeysPub, client, usr)
require.NoError(t, err)
require.Equal(t, user.StatusOK, st)
result := services.Verify(context.TODO(), services.KeysPub, client, usr)
require.Equal(t, user.StatusOK, result.Status)
expected := `BEGIN MESSAGE.
kdZaJI1U5AS7G6i VoUxdP8OtPzEoM6 pYhVl0YQZJnotVE wLg9BDb5SUO05pm
abUSeCvBfdPoRpP J8wrcF5PP3wTCKq 6Xr2MZHgg6m2Qal gJCD6vMqlBQfIg6
QsfB27aP5DMuXlJ AUVIAvMDHIoptmS riNMzfpwBjRShVL WH70a0GOEqD6L8b
kC5EFOwCedvHFpc AQVqULHjcSpeCfZ EIOaQ2IP.
END MESSAGE.`
require.Equal(t, expected, msg)
require.Equal(t, expected, result.Statement)
}
7 changes: 3 additions & 4 deletions user/services/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ func TestProxy(t *testing.T) {
usr, err := user.New(kid, "twitter", "gabrlh", urs, 1)
require.NoError(t, err)
client := http.NewClient()
st, msg, err := services.RequestVerify(context.TODO(), services.Proxy, client, usr)
require.NoError(t, err)
require.Equal(t, user.StatusOK, st)
result := services.Verify(context.TODO(), services.Proxy, client, usr)
require.Equal(t, user.StatusOK, result.Status)
expected := "BEGIN MESSAGE.\nEqcgDt8RfXvPq9b 4qCV8S3VPKIQKqa N7Rc1YruQQYuVS8 niHzUv7jdykkEPSrKGcJQCNTkNE7uF swPuwfpaZX6TCKq 6Xr2MZHgg6S0Mjg WFMJ1KHxazTuXs4icK3k8SZCR8mVLQ MSVhFeMrvz0qJOm A96zW9RAY6whsLo 5fC8i3fRJjyo9mQJZid8MwBXJl1XDL 5ZOSkLYs6sk6a2g CiGyA2IP.\nEND MESSAGE."
require.Equal(t, expected, msg)
require.Equal(t, expected, result.Statement)
}
7 changes: 3 additions & 4 deletions user/services/reddit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ func TestReddit(t *testing.T) {
usr, err := user.New(kid, "reddit", "gabrlh", "https://www.reddit.com/r/keyspubmsgs/comments/f8g9vd/gabrlh/", 1)
require.NoError(t, err)
client := http.NewClient()
st, msg, err := services.RequestVerify(context.TODO(), services.Reddit, client, usr)
require.NoError(t, err)
require.Equal(t, user.StatusOK, st)
result := services.Verify(context.TODO(), services.Reddit, client, usr)
require.Equal(t, user.StatusOK, result.Status)
expected := `BEGIN MESSAGE.
tm8882H30GKybLj cOvOw3ezalNCV4z HIeF7ZIDa53DM5l m43v3AdpuM5xtqTZDGIhyQbA863bYk fiIRdpUYVzMTCKq 6Xr2MZHgg4bh2Wj m5fbDX2FnO9rt6TWzS6zMQo6Pf4PXS De2cdyxT0J3mPah X4cThM1A4yFIFaF lo99DSnDd3LOLwUrP9mdKCnNdvKkl1 WLZZaBlQZWXAisM CCwny21.
END MESSAGE.`
require.Equal(t, expected, msg)
require.Equal(t, expected, result.Statement)
}
5 changes: 2 additions & 3 deletions user/services/twitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ func TestTwitter(t *testing.T) {
usr, err := user.New(kid, "twitter", "gabrlh", urs, 1)
require.NoError(t, err)
client := http.NewClient()
st, _, err := services.RequestVerify(context.TODO(), services.Twitter, client, usr)
require.NoError(t, err)
require.Equal(t, user.StatusOK, st)
result := services.Verify(context.TODO(), services.Twitter, client, usr)
require.Equal(t, user.StatusOK, result.Status)
// TODO: Require msg
}
55 changes: 32 additions & 23 deletions user/services/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,23 @@ import (
"github.com/pkg/errors"
)

// RequestVerify get user URL using client and verifies it.
// If there is an error, it is set on the result.
func RequestVerify(ctx context.Context, service Service, client http.Client, usr *user.User) (user.Status, string, error) {
st, body, err := service.Request(ctx, client, usr)
if err != nil {
return st, "", err
}
if st != user.StatusOK {
return st, "", err
}
if body == nil {
return user.StatusResourceNotFound, "", errors.Errorf("resource not found")
}

st, msg, err := service.Verify(ctx, body, usr)
if err != nil {
logger.Warningf("Failed to check content: %s", err)
return st, "", err
}

return st, msg, nil
// Verify a user.
// The result.Status is success (StatusOK) or type of failure.
// If a failure, result.Err has the error message.
func Verify(ctx context.Context, service Service, client http.Client, usr *user.User) *user.Result {
result := &user.Result{User: usr}
UpdateResult(ctx, service, result, client, time.Now())
return result
}

// UpdateResult updates a user.Result using client.
// UpdateResult updates a user.Result.
// The result.Status is success (StatusOK) or type of failure.
// If a failure, result.Err has the error message.
func UpdateResult(ctx context.Context, service Service, result *user.Result, client http.Client, now time.Time) {
logger.Infof("Update user %s", result.User.String())

result.Timestamp = tsutil.Millis(now)
st, msg, err := RequestVerify(ctx, service, client, result.User)
st, msg, err := requestVerify(ctx, service, client, result.User)
if err != nil {
result.Err = err.Error()
result.Status = st
Expand All @@ -54,3 +40,26 @@ func UpdateResult(ctx context.Context, service Service, result *user.Result, cli
result.Status = st
result.VerifiedAt = tsutil.Millis(now)
}

// requestVerify get user URL using client and verifies it.
// If there is an error, it is set on the result.
func requestVerify(ctx context.Context, service Service, client http.Client, usr *user.User) (user.Status, string, error) {
st, body, err := service.Request(ctx, client, usr)
if err != nil {
return st, "", err
}
if st != user.StatusOK {
return st, "", err
}
if body == nil {
return user.StatusResourceNotFound, "", errors.Errorf("resource not found")
}

st, msg, err := service.Verify(ctx, body, usr)
if err != nil {
logger.Warningf("Failed to check content: %s", err)
return st, "", err
}

return st, msg, nil
}

0 comments on commit 926fad6

Please sign in to comment.