Skip to content

Commit

Permalink
Implement tests for HasGravatar function
Browse files Browse the repository at this point in the history
  • Loading branch information
Helielson authored and Steven Wolfe committed Oct 24, 2017
1 parent 8d2d357 commit abc202d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NOVENDOR_PATH = $$(glide novendor)

runtest:
go clean
go test ${NOVENDOR_PATH}
18 changes: 11 additions & 7 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ import:
subpackages:
- errgroup
- syncmap
testImport:
- package: gopkg.in/check.v1
- package: gopkg.in/h2non/gock.v1
63 changes: 63 additions & 0 deletions verifier/gravatar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package verifier

import (
"errors"
"net/http"
"testing"

"gopkg.in/check.v1"
"gopkg.in/h2non/gock.v1"
)

type gravatarSuite struct{}

var _ = check.Suite(&gravatarSuite{})

func Test(t *testing.T) { check.TestingT(t) } // Just to make discoverable

func configureRequestMock(addressMD5 string, statusCode int) *gock.Response {
url := "https://en.gravatar.com/" + addressMD5 + ".json"
return gock.New(url).
Head("").
Reply(statusCode)
}

func (s *gravatarSuite) TestHasGravatarStatusOk(c *check.C) {
address := &Address{
Username: "username",
Domain: "domain.com",
Address: "[email protected]",
}

configureRequestMock(address.MD5(), http.StatusOK)
defer gock.Off()

c.Assert(HasGravatar(address), check.Equals, true)
}

func (s *gravatarSuite) TestHasGravatarRequestError(c *check.C) {
address := &Address{
Username: "username",
Domain: "domain.com",
Address: "[email protected]",
}

gockResponse := configureRequestMock(address.MD5(), 200)
gockResponse.SetError(errors.New("Some error while requesting"))
defer gock.Off()

c.Assert(HasGravatar(address), check.Equals, false)
}

func (s *gravatarSuite) TestHasGravatarStatusNotOk(c *check.C) {
address := &Address{
Username: "username",
Domain: "domain.com",
Address: "[email protected]",
}

configureRequestMock(address.MD5(), http.StatusBadRequest)
defer gock.Off()

c.Assert(HasGravatar(address), check.Equals, false)
}

0 comments on commit abc202d

Please sign in to comment.