Skip to content

Commit

Permalink
Implement + Test [GET] /v1/me/followers/
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonRomano committed Dec 16, 2016
1 parent d088b6c commit ba7a0d9
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
50 changes: 50 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,3 +822,53 @@ func (suite *ClientTestSuite) TestTimeoutMeBoardsSuggestedFetch() {
)
assert.NotEqual(suite.T(), nil, err)
}

// ========================================
// ========== Me.Followers.Fetch ==========
// ========================================

// TestSuccessfulMeFollowersFetch tests that we can fetch
// followers of the authorized user.
func (suite *ClientTestSuite) TestSuccessfulMeFollowersFetch() {
// Test simple fetch
users, page, err := suite.client.Me.Followers.Fetch(
&controllers.MeFollowersFetchOptionals{},
)
assert.Equal(suite.T(), nil, err)
assert.Equal(suite.T(), len(*users), 25)

// Load second page
users, page, err = suite.client.Me.Followers.Fetch(
&controllers.MeFollowersFetchOptionals{
Cursor: page.Cursor,
},
)
assert.Equal(suite.T(), nil, err)
assert.True(suite.T(), len(*users) > 0)
}

// TestTimeoutMeFollowersFetch tests that an error is appropriately thrown
// when a network timeout occurs
func (suite *ClientTestSuite) TestTimeoutMeFollowersFetch() {
_, _, err := suite.timeoutClient.Me.Followers.Fetch(
&controllers.MeFollowersFetchOptionals{},
)
assert.NotEqual(suite.T(), nil, err)
}

// TestUnauthorizedMeFollowersFetch tests that a 401 is thrown
// when an unauthorized user tries to call a /me endpoint
func (suite *ClientTestSuite) TestUnauthorizedMeFollowersFetch() {
_, _, err := suite.unauthorizedClient.Me.Followers.Fetch(
&controllers.MeFollowersFetchOptionals{},
)

// Check error type
if pinterestError, ok := err.(*models.PinterestError); ok {
// Should be a 401
assert.Equal(suite.T(), http.StatusUnauthorized, pinterestError.StatusCode)
} else {
// Make this error out, should always be a PinterestError
assert.Equal(suite.T(), true, false)
}
}
40 changes: 38 additions & 2 deletions controllers/me_followers_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"github.com/BrandonRomano/wrecker"
"github.com/carrot/go-pinterest/models"
)

// MeFollowersController is the controller that is responsible for all
Expand All @@ -17,6 +18,41 @@ func newMeFollowersController(wc *wrecker.Wrecker) *MeFollowersController {
}
}

func (*MeFollowersController) Fetch() {
// TODO
// MeFollowersFetchOptionals is a struct that represents the optional
// parameters for the Fetch method
type MeFollowersFetchOptionals struct {
Cursor string
}

// Fetch loads the users that follow the logged in user
// Endpoint: [GET] /v1/me/boards/followers/
func (mfc *MeFollowersController) Fetch(optionals *MeFollowersFetchOptionals) (*[]models.User, *models.Page, error) {
// Build request
response := new(models.Response)
response.Data = &[]models.User{}
request := mfc.wreckerClient.Get("/me/followers/").
URLParam("fields", "first_name,id,last_name,url,account_type,bio,counts,created_at,image,username").
Into(response)
if optionals.Cursor != "" {
request.URLParam("cursor", optionals.Cursor)
}

// Execute request
resp, err := request.Execute()

// Error from Wrecker
if err != nil {
return nil, nil, err
}

// Status code
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return nil, nil, &models.PinterestError{
StatusCode: resp.StatusCode,
Message: response.Message,
}
}

// OK
return response.Data.(*[]models.User), &response.Page, nil
}

0 comments on commit ba7a0d9

Please sign in to comment.