File size: 1,543 Bytes
2c8c8ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package boosty

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/suite"
	"gohome.4gophers.ru/getapp/boosty/auth"
	"gohome.4gophers.ru/getapp/boosty/request"
)

type CurrentTestSuite struct {
	suite.Suite
}

func (s *CurrentTestSuite) SetupTest() {
	//
}

func (s *BoostyTestSuite) TestStats() {
	tests := map[string]struct {
		followersCount int
		paidCount      int
		body           string
		token          string
	}{
		"success stats": {
			followersCount: 0, paidCount: 0, body: currentBody, token: "123",
		},
	}

	for name, test := range tests {
		s.T().Run(name, func(t *testing.T) {
			svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				auth := r.Header.Get("Authorization")

				s.Assert().Equal(auth, "Bearer "+test.token)

				fmt.Fprintf(w, test.body)
			}))
			defer svr.Close()

			auth, err := auth.New(auth.WithInfo(auth.Info{
				AccessToken: test.token,
			}))
			s.Assert().NoError(err)

			req, err := request.New(
				request.WithUrl(svr.URL),
				request.WithAuth(auth),
				request.WithClient(&http.Client{}),
			)
			s.Assert().NoError(err)

			b, err := New("", WithRequest(req))
			s.Assert().NoError(err)

			stats, err := b.Current()

			s.Assert().NoError(err)
			s.Assert().Equal(test.followersCount, stats.FollowersCount)
			s.Assert().Equal(test.paidCount, stats.FollowersCount)

		})
	}
}

const currentBody = `
{
  "followersCount": 0,
  "income": 6210,
  "balance": 0,
  "payoutSum": 6210,
  "paidCount": 2,
  "hold": 0
}
`