package tests

import (
	"net/http"
	"net/url"
	"wechat-conf/controllers"
	"wechat-conf/utils"
	"testing"
	"time"

	"github.com/astaxie/beego"

	. "github.com/smartystreets/goconvey/convey"
)

func TestPreCourseOrder(t *testing.T) {
	Convey("课程下单", t, func() {

		Convey("正常下单", func() {
			params := url.Values{}
			params.Add("info", `{"CourseId":"ecc55f91-52b4-4e9b-89a8-b9b3362730f9","CaseId":"10","Price":"2.00"}`)
			result := &controllers.JSONMessage{}
			api := "/api/wechat/MQ/order/course"

			token := &utils.JWTToken{
				Guest:   false,
				ID:      "oMOpz0kgTrasoAA3G70R7phomn1g", // openid
				Expire:  time.Now().Local().Add(24 * 30 * time.Hour),
				BatchNo: "",
			}

			code, _, err := NewRequestMock().
				AddToken(token.ToMap()).
				AddWechatUA().
				Request(http.MethodPost, api, params, result)

			beego.Trace(result)

			So(code, ShouldEqual, http.StatusOK)
			So(err, ShouldBeNil)

			// 业务返回判断
			So(result.Code, ShouldEqual, http.StatusOK)
			So(result.Result, ShouldNotBeEmpty)

			orderID := result.Result.(map[string]interface{})["OrdersId"].(string)

			Convey("下单确认", func() {
				result := &controllers.JSONMessage{}
				api := "/api/wechat/MQ/order/course/" + orderID

				token := &utils.JWTToken{
					Guest:   false,
					ID:      "oMOpz0kgTrasoAA3G70R7phomn1g", // openid
					Expire:  time.Now().Local().Add(24 * 30 * time.Hour),
					BatchNo: "",
				}

				code, _, err := NewRequestMock().
					AddToken(token.ToMap()).
					AddWechatUA().
					Request(http.MethodPut, api, nil, result)

				beego.Trace(result)

				So(code, ShouldEqual, http.StatusOK)
				So(err, ShouldBeNil)

				// 业务返回判断
				So(result.Code, ShouldEqual, http.StatusOK)
			})
		})
	})
}