Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package api
- import (
- "bytes"
- "encoding/json"
- "github.com/gin-gonic/gin"
- _ "github.com/jinzhu/gorm/dialects/sqlite"
- . "github.com/smartystreets/goconvey/convey"
- "github.com/stretchr/testify/assert"
- "mastersapi/database"
- "mastersapi/middleware"
- "mastersapi/models"
- "mastersapi/test"
- "net/http"
- "net/http/httptest"
- "os"
- "testing"
- )
- func TestMain(m *testing.M) {
- //SetupTests()
- gin.SetMode(gin.ReleaseMode)
- retCode := m.Run()
- os.Exit(retCode)
- }
- func TestRegister(t *testing.T) {
- test.InitDb("register")
- defer test.CloseDb("register")
- email := "chirkin.ivan@gmail.com"
- pass := "123456"
- Convey("Check POST /register method", t, func() {
- r := Setup(middleware.Auth())
- w := httptest.NewRecorder()
- models.PassCost = 1
- data, _ := json.Marshal(RegisterForm{
- Email: email,
- Password: pass,
- })
- req, _ := http.NewRequest("POST", "/register", bytes.NewBuffer(data))
- r.ServeHTTP(w, req)
- type RegisterResponse struct {
- Success bool
- Code int
- Data struct {
- Email string
- }
- }
- var response RegisterResponse
- err := json.Unmarshal([]byte(w.Body.String()), &response)
- So(err, ShouldBeNil)
- Convey("Good request", func() {
- Println(w.Body.String())
- So(w.Code, ShouldEqual, http.StatusOK)
- So(response.Code, ShouldEqual, http.StatusOK)
- So(response.Data.Email, ShouldEqual, email)
- account := models.Account{}
- count := database.Get().First(&account, 1).RowsAffected
- So(count, ShouldEqual, int64(1))
- })
- Convey("Email uniq check", func() {
- Println(w.Body.String())
- So(w.Code, ShouldEqual, http.StatusBadRequest)
- So(response.Code, ShouldEqual, http.StatusBadRequest)
- Reset(func() {
- email = ""
- pass = "12345"
- })
- })
- Convey("Empty email", func() {
- Println(w.Body.String())
- So(w.Code, ShouldEqual, http.StatusBadRequest)
- So(response.Code, ShouldEqual, http.StatusBadRequest)
- Reset(func() {
- email = "chirkin.ivan@gmail.com"
- pass = ""
- })
- })
- Convey("Empty pass", func() {
- Println(w.Body.String())
- So(w.Code, ShouldEqual, http.StatusBadRequest)
- So(response.Code, ShouldEqual, http.StatusBadRequest)
- Reset(func() {
- email = "badEmail"
- pass = "12345"
- })
- })
- Convey("Bad email", func() {
- Println(w.Body.String())
- So(w.Code, ShouldEqual, http.StatusBadRequest)
- So(response.Code, ShouldEqual, http.StatusBadRequest)
- Reset(func() {
- email = "mail@test.ru"
- pass = "12345"
- test.CloseDb("register")
- test.InitDbWithoutMigrate("register")
- })
- })
- Convey("Without table InternalError", func() {
- Println(w.Body.String())
- So(w.Code, ShouldEqual, http.StatusInternalServerError)
- So(response.Code, ShouldEqual, http.StatusInternalServerError)
- })
- })
- }
- func TestLogin(t *testing.T) {
- test.InitDb("login")
- defer test.CloseDb("login")
- email := "test@mail.ru"
- account := models.Account{
- Email: email,
- LoginErrors: 3,
- }
- password := "123"
- account.SetPassword(password)
- err := database.Get().Create(&account).Error
- assert.Nil(t, err)
- r := Setup(middleware.Auth())
- Convey("Test POST /login method", t, func() {
- w := httptest.NewRecorder()
- data, _ := json.Marshal(LoginForm{
- Email: email,
- Password: password,
- })
- req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer(data))
- r.ServeHTTP(w, req)
- Convey("Good login", func() {
- So(w.Code, ShouldEqual, http.StatusOK)
- type GoodLoginResponse struct {
- Success bool
- Code int
- Data struct {
- Key string
- }
- }
- var response GoodLoginResponse
- err := json.Unmarshal([]byte(w.Body.String()), &response)
- So(err, ShouldBeNil)
- session := models.AccountSession{}
- notFound := database.Get().First(&session, 1).RecordNotFound()
- So(notFound, ShouldBeFalse)
- So(response.Data.Key, ShouldEqual, session.Key)
- err = database.Get().First(&account, 1).Error
- So(err,ShouldBeNil)
- So(account.LoginErrors, ShouldEqual, 0)
- Reset(func() {
- account.LoginErrors = 6
- err = database.Get().Save(&account).Error
- So(err, ShouldBeNil)
- password = "wrong_pass"
- })
- })
- Convey("Bad login", func() {
- So(w.Code, ShouldEqual, http.StatusForbidden)
- type BadLoginResponse struct {
- Success bool
- Code int
- }
- var response BadLoginResponse
- err = json.Unmarshal([]byte(w.Body.String()), &response)
- So(err, ShouldBeNil)
- So(response.Code, ShouldEqual, http.StatusForbidden)
- var account models.Account
- err = database.Get().First(&account, 1).Error
- So(err, ShouldBeNil)
- So(account.LoginErrors, ShouldEqual, 7)
- Reset(func() {
- password = ""
- })
- })
- Convey("Without pass", func() {
- So(w.Code, ShouldEqual, http.StatusBadRequest)
- Reset(func() {
- email = ""
- password = "123"
- })
- })
- Convey("Without email", func() {
- So(w.Code, ShouldEqual, http.StatusBadRequest)
- Reset(func() {
- email = "badEmail"
- password = "123"
- })
- })
- Convey("With bad email", func() {
- So(w.Code, ShouldEqual, http.StatusBadRequest)
- Reset(func() {
- email = "test@mail.ru"
- password = "123"
- err := database.Get().DropTable("accounts_sessions").Error
- So(err, ShouldBeNil)
- })
- })
- Convey("Without table sessions", func() {
- Println(w.Body.String())
- So(w.Code, ShouldEqual, http.StatusInternalServerError)
- Reset(func() {
- email = "test@mail.ru"
- password = "123"
- err := database.Get().DropTable("accounts").Error
- So(err, ShouldBeNil)
- })
- })
- Convey("Without table account", func() {
- Println(w.Body.String())
- So(w.Code, ShouldEqual, http.StatusInternalServerError)
- })
- })
- }
- func TestLogout(t *testing.T) {
- test.InitDb("logout")
- defer test.CloseDb("logout")
- r := Setup(middleware.Auth())
- token := test.Token
- test.CreateSession()
- Convey("Test GET /logout method", t, func() {
- w := httptest.NewRecorder()
- req, _ := http.NewRequest("GET", "/logout", nil)
- req.Header.Set("X-Token", token)
- r.ServeHTTP(w, req)
- Convey("Good request", func() {
- So(w.Code, ShouldEqual, http.StatusOK)
- var response SuccessResponse
- err := json.Unmarshal([]byte(w.Body.String()), &response)
- So(err, ShouldBeNil)
- So(response.Code, ShouldEqual, http.StatusOK)
- Reset(func() {
- token = "bad-token"
- })
- })
- Convey("Bad request", func() {
- So(w.Code, ShouldEqual, http.StatusForbidden)
- var response FailureResponse
- err := json.Unmarshal([]byte(w.Body.String()), &response)
- So(err, ShouldBeNil)
- So(response.Code, ShouldEqual, http.StatusForbidden)
- })
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement