Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. package api
  2.  
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "github.com/gin-gonic/gin"
  7. _ "github.com/jinzhu/gorm/dialects/sqlite"
  8. . "github.com/smartystreets/goconvey/convey"
  9. "github.com/stretchr/testify/assert"
  10. "mastersapi/database"
  11. "mastersapi/middleware"
  12. "mastersapi/models"
  13. "mastersapi/test"
  14. "net/http"
  15. "net/http/httptest"
  16. "os"
  17. "testing"
  18. )
  19.  
  20.  
  21. func TestMain(m *testing.M) {
  22. //SetupTests()
  23. gin.SetMode(gin.ReleaseMode)
  24. retCode := m.Run()
  25. os.Exit(retCode)
  26. }
  27.  
  28. func TestRegister(t *testing.T) {
  29. test.InitDb("register")
  30. defer test.CloseDb("register")
  31.  
  32. email := "chirkin.ivan@gmail.com"
  33. pass := "123456"
  34.  
  35. Convey("Check POST /register method", t, func() {
  36. r := Setup(middleware.Auth())
  37. w := httptest.NewRecorder()
  38.  
  39. models.PassCost = 1
  40.  
  41. data, _ := json.Marshal(RegisterForm{
  42. Email: email,
  43. Password: pass,
  44. })
  45. req, _ := http.NewRequest("POST", "/register", bytes.NewBuffer(data))
  46. r.ServeHTTP(w, req)
  47.  
  48. type RegisterResponse struct {
  49. Success bool
  50. Code int
  51. Data struct {
  52. Email string
  53. }
  54. }
  55.  
  56. var response RegisterResponse
  57.  
  58. err := json.Unmarshal([]byte(w.Body.String()), &response)
  59. So(err, ShouldBeNil)
  60.  
  61. Convey("Good request", func() {
  62. Println(w.Body.String())
  63.  
  64. So(w.Code, ShouldEqual, http.StatusOK)
  65. So(response.Code, ShouldEqual, http.StatusOK)
  66. So(response.Data.Email, ShouldEqual, email)
  67.  
  68. account := models.Account{}
  69. count := database.Get().First(&account, 1).RowsAffected
  70. So(count, ShouldEqual, int64(1))
  71. })
  72.  
  73. Convey("Email uniq check", func() {
  74. Println(w.Body.String())
  75. So(w.Code, ShouldEqual, http.StatusBadRequest)
  76. So(response.Code, ShouldEqual, http.StatusBadRequest)
  77.  
  78. Reset(func() {
  79. email = ""
  80. pass = "12345"
  81. })
  82. })
  83.  
  84. Convey("Empty email", func() {
  85. Println(w.Body.String())
  86. So(w.Code, ShouldEqual, http.StatusBadRequest)
  87. So(response.Code, ShouldEqual, http.StatusBadRequest)
  88.  
  89. Reset(func() {
  90. email = "chirkin.ivan@gmail.com"
  91. pass = ""
  92. })
  93. })
  94.  
  95. Convey("Empty pass", func() {
  96. Println(w.Body.String())
  97. So(w.Code, ShouldEqual, http.StatusBadRequest)
  98. So(response.Code, ShouldEqual, http.StatusBadRequest)
  99.  
  100. Reset(func() {
  101. email = "badEmail"
  102. pass = "12345"
  103. })
  104. })
  105.  
  106. Convey("Bad email", func() {
  107. Println(w.Body.String())
  108. So(w.Code, ShouldEqual, http.StatusBadRequest)
  109. So(response.Code, ShouldEqual, http.StatusBadRequest)
  110.  
  111. Reset(func() {
  112. email = "mail@test.ru"
  113. pass = "12345"
  114. test.CloseDb("register")
  115. test.InitDbWithoutMigrate("register")
  116. })
  117. })
  118.  
  119. Convey("Without table InternalError", func() {
  120. Println(w.Body.String())
  121. So(w.Code, ShouldEqual, http.StatusInternalServerError)
  122. So(response.Code, ShouldEqual, http.StatusInternalServerError)
  123. })
  124. })
  125. }
  126.  
  127.  
  128. func TestLogin(t *testing.T) {
  129. test.InitDb("login")
  130. defer test.CloseDb("login")
  131.  
  132. email := "test@mail.ru"
  133. account := models.Account{
  134. Email: email,
  135. LoginErrors: 3,
  136. }
  137.  
  138. password := "123"
  139. account.SetPassword(password)
  140.  
  141. err := database.Get().Create(&account).Error
  142. assert.Nil(t, err)
  143.  
  144. r := Setup(middleware.Auth())
  145.  
  146. Convey("Test POST /login method", t, func() {
  147.  
  148. w := httptest.NewRecorder()
  149. data, _ := json.Marshal(LoginForm{
  150. Email: email,
  151. Password: password,
  152. })
  153. req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer(data))
  154. r.ServeHTTP(w, req)
  155.  
  156. Convey("Good login", func() {
  157. So(w.Code, ShouldEqual, http.StatusOK)
  158.  
  159. type GoodLoginResponse struct {
  160. Success bool
  161. Code int
  162. Data struct {
  163. Key string
  164. }
  165. }
  166.  
  167. var response GoodLoginResponse
  168.  
  169. err := json.Unmarshal([]byte(w.Body.String()), &response)
  170. So(err, ShouldBeNil)
  171.  
  172. session := models.AccountSession{}
  173.  
  174. notFound := database.Get().First(&session, 1).RecordNotFound()
  175. So(notFound, ShouldBeFalse)
  176. So(response.Data.Key, ShouldEqual, session.Key)
  177.  
  178. err = database.Get().First(&account, 1).Error
  179. So(err,ShouldBeNil)
  180. So(account.LoginErrors, ShouldEqual, 0)
  181.  
  182. Reset(func() {
  183. account.LoginErrors = 6
  184. err = database.Get().Save(&account).Error
  185. So(err, ShouldBeNil)
  186.  
  187. password = "wrong_pass"
  188. })
  189. })
  190.  
  191. Convey("Bad login", func() {
  192. So(w.Code, ShouldEqual, http.StatusForbidden)
  193.  
  194. type BadLoginResponse struct {
  195. Success bool
  196. Code int
  197. }
  198.  
  199. var response BadLoginResponse
  200.  
  201. err = json.Unmarshal([]byte(w.Body.String()), &response)
  202. So(err, ShouldBeNil)
  203.  
  204. So(response.Code, ShouldEqual, http.StatusForbidden)
  205.  
  206. var account models.Account
  207. err = database.Get().First(&account, 1).Error
  208. So(err, ShouldBeNil)
  209.  
  210. So(account.LoginErrors, ShouldEqual, 7)
  211.  
  212. Reset(func() {
  213. password = ""
  214. })
  215. })
  216.  
  217. Convey("Without pass", func() {
  218. So(w.Code, ShouldEqual, http.StatusBadRequest)
  219. Reset(func() {
  220. email = ""
  221. password = "123"
  222. })
  223. })
  224.  
  225. Convey("Without email", func() {
  226. So(w.Code, ShouldEqual, http.StatusBadRequest)
  227. Reset(func() {
  228. email = "badEmail"
  229. password = "123"
  230. })
  231. })
  232.  
  233. Convey("With bad email", func() {
  234. So(w.Code, ShouldEqual, http.StatusBadRequest)
  235. Reset(func() {
  236. email = "test@mail.ru"
  237. password = "123"
  238.  
  239. err := database.Get().DropTable("accounts_sessions").Error
  240. So(err, ShouldBeNil)
  241. })
  242. })
  243.  
  244. Convey("Without table sessions", func() {
  245. Println(w.Body.String())
  246. So(w.Code, ShouldEqual, http.StatusInternalServerError)
  247. Reset(func() {
  248. email = "test@mail.ru"
  249. password = "123"
  250. err := database.Get().DropTable("accounts").Error
  251. So(err, ShouldBeNil)
  252. })
  253. })
  254.  
  255. Convey("Without table account", func() {
  256. Println(w.Body.String())
  257. So(w.Code, ShouldEqual, http.StatusInternalServerError)
  258. })
  259. })
  260.  
  261. }
  262.  
  263. func TestLogout(t *testing.T) {
  264. test.InitDb("logout")
  265. defer test.CloseDb("logout")
  266.  
  267. r := Setup(middleware.Auth())
  268.  
  269. token := test.Token
  270. test.CreateSession()
  271.  
  272. Convey("Test GET /logout method", t, func() {
  273. w := httptest.NewRecorder()
  274. req, _ := http.NewRequest("GET", "/logout", nil)
  275. req.Header.Set("X-Token", token)
  276. r.ServeHTTP(w, req)
  277.  
  278. Convey("Good request", func() {
  279. So(w.Code, ShouldEqual, http.StatusOK)
  280.  
  281.  
  282. var response SuccessResponse
  283.  
  284. err := json.Unmarshal([]byte(w.Body.String()), &response)
  285. So(err, ShouldBeNil)
  286.  
  287. So(response.Code, ShouldEqual, http.StatusOK)
  288.  
  289. Reset(func() {
  290. token = "bad-token"
  291. })
  292. })
  293.  
  294. Convey("Bad request", func() {
  295. So(w.Code, ShouldEqual, http.StatusForbidden)
  296.  
  297. var response FailureResponse
  298.  
  299. err := json.Unmarshal([]byte(w.Body.String()), &response)
  300. So(err, ShouldBeNil)
  301.  
  302. So(response.Code, ShouldEqual, http.StatusForbidden)
  303.  
  304. })
  305. })
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement