v1_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package v1_test
  2. import (
  3. "net/http"
  4. "testing"
  5. "time"
  6. . "github.com/onsi/ginkgo"
  7. . "github.com/onsi/gomega"
  8. v1 "github.com/vladimirok5959/golang-donatello/donatello/v1"
  9. "golang.org/x/net/context"
  10. )
  11. const MockMeData = `{
  12. "nickname": "NickName",
  13. "pubId": "A1B-A123456",
  14. "page": "https://donatello.to/nickname",
  15. "isActive": true,
  16. "isPublic": true,
  17. "donates": {
  18. "totalAmount": 100,
  19. "totalCount": 1
  20. },
  21. "createdAt": "2022-10-20 00:30:50"
  22. }`
  23. const MockDonatesData = `{
  24. "content": [
  25. {
  26. "pubId": "A1B-A123456",
  27. "clientName": "ClientName",
  28. "message": "Message",
  29. "amount": "100",
  30. "currency": "UAH",
  31. "isPublished": false,
  32. "createdAt": "2022-10-20 00:30:50"
  33. }
  34. ],
  35. "page": 1,
  36. "size": 20,
  37. "pages": 1,
  38. "first": true,
  39. "last": true,
  40. "total": 1
  41. }`
  42. const MockClientsData = `{
  43. "clients": [
  44. {
  45. "clientName": "ClientName",
  46. "totalAmount": 100
  47. }
  48. ]
  49. }`
  50. var _ = Describe("Client", func() {
  51. Context("Func", func() {
  52. var api *v1.ClientFakeAPI
  53. var client *v1.Client
  54. var ctx context.Context
  55. BeforeEach(func() {
  56. api = v1.NewClientFakeAPI()
  57. client = v1.NewClient(api)
  58. ctx = context.Background()
  59. })
  60. Context("Me", func() {
  61. It("respond as unauthorized", func() {
  62. resp, err := client.Me(ctx)
  63. Expect(err).To(Succeed())
  64. Expect(resp.Success).To(BeFalse())
  65. Expect(resp.Message).To(Equal("Помилка авторизації"))
  66. })
  67. It("respond with correct data", func() {
  68. api.MockMe = func() (int64, []byte, error) {
  69. return http.StatusOK, []byte(MockMeData), nil
  70. }
  71. resp, err := client.Me(ctx)
  72. Expect(err).To(Succeed())
  73. Expect(resp.Success).To(BeTrue())
  74. Expect(resp.Message).To(Equal(""))
  75. Expect(resp.NickName).To(Equal("NickName"))
  76. Expect(resp.PubID).To(Equal("A1B-A123456"))
  77. Expect(resp.Page).To(Equal("https://donatello.to/nickname"))
  78. Expect(resp.IsActive).To(BeTrue())
  79. Expect(resp.IsPublic).To(BeTrue())
  80. Expect(resp.Donates).To(Equal(v1.ResponseMeDonates{
  81. TotalAmount: 100,
  82. TotalCount: 1,
  83. }))
  84. Expect(resp.CreatedAt).To(Equal("2022-10-20 00:30:50"))
  85. Expect(resp.CreatedAtTime()).To(Equal(time.Date(2022, time.October, 20, 0, 30, 50, 0, time.UTC)))
  86. })
  87. })
  88. Context("Donates", func() {
  89. It("respond as unauthorized", func() {
  90. resp, err := client.Donates(ctx, 1, 20)
  91. Expect(err).To(Succeed())
  92. Expect(resp.Success).To(BeFalse())
  93. Expect(resp.Message).To(Equal("Помилка авторизації"))
  94. })
  95. It("respond with correct data", func() {
  96. api.MockDonates = func(page, size int64) (int64, []byte, error) {
  97. return http.StatusOK, []byte(MockDonatesData), nil
  98. }
  99. resp, err := client.Donates(ctx, 1, 20)
  100. Expect(err).To(Succeed())
  101. Expect(resp.Success).To(BeTrue())
  102. Expect(resp.Message).To(Equal(""))
  103. Expect(resp.Content).To(Equal([]v1.ResponseDonatesContent{
  104. {
  105. PubID: "A1B-A123456",
  106. ClientName: "ClientName",
  107. Message: "Message",
  108. Amount: "100",
  109. Currency: "UAH",
  110. Goal: "",
  111. IsPublished: false,
  112. CreatedAt: "2022-10-20 00:30:50",
  113. },
  114. }))
  115. Expect(resp.Content[0].AmountInt64()).To(Equal(int64(100)))
  116. Expect(resp.Content[0].CreatedAt).To(Equal("2022-10-20 00:30:50"))
  117. Expect(resp.Content[0].CreatedAtTime()).To(Equal(time.Date(2022, time.October, 20, 0, 30, 50, 0, time.UTC)))
  118. Expect(resp.Page).To(Equal(int64(1)))
  119. Expect(resp.Size).To(Equal(int64(20)))
  120. Expect(resp.Pages).To(Equal(int64(1)))
  121. Expect(resp.First).To(BeTrue())
  122. Expect(resp.Last).To(BeTrue())
  123. Expect(resp.Total).To(Equal(int64(1)))
  124. })
  125. })
  126. Context("Clients", func() {
  127. It("respond as unauthorized", func() {
  128. resp, err := client.Clients(ctx)
  129. Expect(err).To(Succeed())
  130. Expect(resp.Success).To(BeFalse())
  131. Expect(resp.Message).To(Equal("Помилка авторизації"))
  132. })
  133. It("respond with correct data", func() {
  134. api.MockClients = func() (int64, []byte, error) {
  135. return http.StatusOK, []byte(MockClientsData), nil
  136. }
  137. resp, err := client.Clients(ctx)
  138. Expect(err).To(Succeed())
  139. Expect(resp.Success).To(BeTrue())
  140. Expect(resp.Message).To(Equal(""))
  141. Expect(resp.Clients).To(Equal([]v1.ResponseClientsClients{
  142. {
  143. ClientName: "ClientName",
  144. TotalAmount: 100,
  145. },
  146. }))
  147. })
  148. })
  149. Context("EachDonate", func() {
  150. It("iterate over all donates", func() {
  151. api.MockDonates = func(page, size int64) (int64, []byte, error) {
  152. return http.StatusOK, []byte(`{
  153. "content": [
  154. {
  155. "pubId": "A1B-A123451",
  156. "clientName": "ClientName 1",
  157. "message": "Message 1",
  158. "amount": "101",
  159. "currency": "UAH",
  160. "isPublished": false,
  161. "createdAt": "2022-10-20 00:30:50"
  162. },
  163. {
  164. "pubId": "A1B-A123452",
  165. "clientName": "ClientName 2",
  166. "message": "Message 2",
  167. "amount": "102",
  168. "currency": "UAH",
  169. "isPublished": false,
  170. "createdAt": "2022-10-20 00:30:50"
  171. }
  172. ],
  173. "page": 1,
  174. "size": 20,
  175. "pages": 1,
  176. "first": true,
  177. "last": true,
  178. "total": 1
  179. }`), nil
  180. }
  181. donates := []v1.ResponseDonatesContent{}
  182. err := client.EachDonate(ctx, func(donate *v1.ResponseDonatesContent) error {
  183. donates = append(donates, *donate)
  184. return nil
  185. })
  186. Expect(err).To(Succeed())
  187. Expect(len(donates)).To(Equal(2))
  188. Expect(donates[0].PubID).To(Equal("A1B-A123451"))
  189. Expect(donates[1].PubID).To(Equal("A1B-A123452"))
  190. })
  191. })
  192. })
  193. })
  194. func TestSuite(t *testing.T) {
  195. RegisterFailHandler(Fail)
  196. RunSpecs(t, "Client")
  197. }