login_test.go 1.7 KB
package auth

import (
	"github.com/go-pg/pg/v10"
	"net/http"

	"github.com/gavv/httpexpect"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01-gateway/pkg/infrastructure/pg"
)

var _ = Describe("用户登录 返回有权限的公司列表", func() {
	var Id int64
	BeforeEach(func() {
		_, err := pG.DB.QueryOne(
			pg.Scan(&Id),
			"INSERT INTO users (user_id,user_type,company_id) VALUES (1,1,1000) RETURNING user_id",
		)
		_, err = pG.DB.QueryOne(
			pg.Scan(&Id),
			`INSERT INTO companys (company_id,company_info) VALUES (1000,'{"company_id":1000,"name":"company"}') RETURNING company_id`,
		)
		_, err = pG.DB.QueryOne(
			pg.Scan(&Id),
			`INSERT INTO user_auth (user_auth_id,users,phone_auth) VALUES (1,ARRAY [1],'{"phone":"18800000001","password":"password"}') RETURNING user_auth_id`,
		)
		Expect(err).NotTo(HaveOccurred())
	})
	Describe("用户登录 返回有权限的公司列表", func() {
		Context("", func() {
			It("", func() {
				httpExpect := httpexpect.New(GinkgoT(), server.URL)
				body := map[string]interface{}{
					"phone":       "18800000001",
					"grantType":   "signInPassword",
					"password":    "password",
					"captcha":     "",
					"credentials": "",
				}
				httpExpect.POST("/auths/login").
					WithJSON(body).
					Expect().
					Status(http.StatusOK).
					JSON().
					Object().
					ContainsKey("code").ValueEqual("code", 0).
					ContainsKey("msg").ValueEqual("msg", "ok").
					ContainsKey("data").Value("data").Object()
			})
		})
	})
	AfterEach(func() {
		_, err := pG.DB.Exec("DELETE FROM users WHERE true")
		_, err = pG.DB.Exec("DELETE FROM companys WHERE true")
		_, err = pG.DB.Exec("DELETE FROM user_auth WHERE true")
		Expect(err).NotTo(HaveOccurred())
	})
})