read_sent_notification_test.go 2.0 KB
package notification

import (
	"fmt"
	"github.com/gavv/httpexpect"
	"github.com/go-pg/pg"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
	pG "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg"
	"net/http"
	"strconv"
	"time"
)

var _ = Describe("读取发送出的通知", func() {
	var notificationId int64
	var sentNotificationId int64
	BeforeEach(func() {
		_, err := pG.DB.QueryOne(
			pg.Scan(&notificationId),
			"INSERT INTO notifications (id, notification_type, notification_title, notification_content, notification_time, external_resource_type, external_resource) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id",
			1, 1, "testNotificationTitle", "testNotificationContent", time.Now(), 1, 1)
		Expect(err).NotTo(HaveOccurred())
		_, err1 := pG.DB.QueryOne(
			pg.Scan(&sentNotificationId),
			"INSERT INTO sent_notifications (id, notification_id, receiver, is_read, read_time) VALUES (?, ?, ?, ?, ?) RETURNING id",
			1, notificationId, &domain.EmployeeInfo{
				Uid: 2499036607974745088,
			}, false, time.Time{})
		Expect(err1).NotTo(HaveOccurred())
	})
	Describe("读取发送出的通知", func() {
		Context("读取正确sentNotificationId的发送出的通知", func() {
			It("读取成功", func() {
				httpExpect := httpexpect.New(GinkgoT(), server.URL)
				body := map[string]interface{}{
				}
				httpExpect.POST(fmt.Sprintf("/notifications/%s/read", strconv.FormatInt(sentNotificationId, 10))).
					WithJSON(body).
					Expect().
					Status(http.StatusOK).
					JSON().
					Object().
					ContainsKey("code").ValueEqual("code", 0).
					ContainsKey("msg").ValueEqual("msg", "ok").
					ContainsKey("data").Value("data").Object().
					ContainsKey("isRead").ValueEqual("isRead", true)
			})
		})
	})
	AfterEach(func() {
		_, err := pG.DB.Exec("DELETE FROM sent_notifications WHERE true")
		Expect(err).NotTo(HaveOccurred())
		_, err1 := pG.DB.Exec("DELETE FROM notifications WHERE true")
		Expect(err1).NotTo(HaveOccurred())
	})
})