update_user_test.go 3.5 KB
package user

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/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)

var _ = Describe("更新", func() {
	var userId int64
	BeforeEach(func() {
		var err error
		_, err = pG.DB.QueryOne(
			pg.Scan(&userId),
			"INSERT INTO \"users\".\"user_base\"(\"user_base_id\", \"user_info\", \"account\", \"password\", \"im\", \"related_user\", \"status\", \"created_at\", \"updated_at\") VALUES (999, '{\"phone\": \"phone\", \"userName\": \"string\"}', 'phone', 'string', '{\"accid\": \"\", \"imToken\": \"\", \"csAccountId\": \"\"}', '{999}', 1, '2021-07-24 10:16:17.680805+08', '2021-07-24 10:16:17.680805+08') RETURNING user_base_id;",
		)
		Expect(err).NotTo(HaveOccurred())

		_, err = pG.DB.QueryOne(
			pg.Scan(&userId),
			"INSERT INTO \"users\".\"user\"(\"user_id\", \"company_id\", \"user_base_id\", \"user_type\", \"user_code\", \"organization_id\", \"department_id\", \"user_org\", \"user_role\", \"favorite_menus\", \"cooperation_info\", \"enable_status\", \"ext\", \"created_at\", \"updated_at\") VALUES (999, 5, 999, 1025, 'ADMIN01', 5, 5, '[{\"orgId\": 5, \"orgName\": \"string1\", \"createdAt\": \"0001-01-01T00:00:00Z\", \"deletedAt\": \"0001-01-01T00:00:00Z\", \"updatedAt\": \"0001-01-01T00:00:00Z\"}]', '[{\"ext\": {\"orgName\": \"string1\"}, \"roleId\": 5, \"roleName\": \"企业管理员\", \"createdAt\": \"0001-01-01T00:00:00Z\", \"updatedAt\": \"0001-01-01T00:00:00Z\"}]', '[]', '{\"cooperationCompany\": \"\", \"cooperationDeadline\": \"0001-01-01T00:00:00Z\"}', 1, '{\"phone\": \"18860183031\", \"depName\": \"string1\", \"orgName\": \"string1\"}', '2021-07-24 10:16:17.680805+08', '2021-07-24 10:16:17.680805+08');\n",
		)
		Expect(err).NotTo(HaveOccurred())

		_, err = pG.DB.QueryOne(
			pg.Scan(&userId),
			"INSERT INTO users.org (org_id,company_id,created_at,updated_at,deleted_at,org_code,org_name,ext,org_status,is_org,parent_id,parent_path) VALUES (999,999,'2021-07-26 08:06:29.3101584+00:00:00','2021-07-26 08:06:29.3101584+00:00:00','0001-01-01 00:00:00+00:00:00','ENTERPRISE01','string1','{}',1,1,0,'');",
		)
		Expect(err).NotTo(HaveOccurred())
	})
	Describe("提交数据更新", func() {
		Context("提交正确的用户数据", func() {
			It("返回更新后的用户数据", func() {
				httpExpect := httpexpect.New(GinkgoT(), server.URL)
				body := map[string]interface{}{
					"organizationId": 999,
					"departmentId":   999,
					//"userOrg":             "array",
					//"userRole":            "array",
					"cooperationCompany": "string",
					//"cooperationDeadline": "datetime",
					"enableStatus": 1,
					"userName":     "string",
					"phone":        "phone",
					"avatar":       "string",
					"email":        "string",
				}
				httpExpect.PUT("/user/999").
					WithJSON(body).
					Expect().
					Status(http.StatusOK).
					JSON().
					Object().
					ContainsKey("code").ValueEqual("code", 0).
					ContainsKey("msg").ValueEqual("msg", "ok").
					ContainsKey("data").Value("data").Object().
					ContainsKey("userId").ValueEqual("userId", userId)
			})
		})
	})
	AfterEach(func() {
		_, err := pG.DB.Exec("DELETE FROM users.user_base WHERE user_base_id = 999")
		Expect(err).NotTo(HaveOccurred())
		_, err = pG.DB.Exec(`DELETE FROM users."user" WHERE user_id = 999`)
		Expect(err).NotTo(HaveOccurred())
		_, err = pG.DB.Exec(`DELETE FROM users."org" WHERE org_id = 999`)
		Expect(err).NotTo(HaveOccurred())
	})
})