package tenant

import (
	"testing"

	"github.com/pingcap/dbaas/central/config"
	. "github.com/smartystreets/goconvey/convey"
	"github.com/tidbcloud/idl/pbgen/proto/central"
)

func TestValidateRegistrationReq(t *testing.T) {
	Convey("Google SSO", t, func() {
		req := &central.RegistrationReq{
			Country:                   "CN",
			FirstName:                 "",
			LastName:                  "",
			Email:                     "",
			Job:                       "",
			Company:                   "TEST",
			PhoneNumber:               "11111111111",
			Password:                  "",
			AcceptService:             true,
			RecaptchaResponse:         "",
			ThirdPartyAccountProvider: "",
			ThirdPartyAccountToken:    "",
			ConnectionType:            central.RegistrationReq_GOOGLE,
			Auth0Token:                "xxxx",
		}

		Convey("should return google connection", func() {
			connectionType, err := getAuth0ConnectionType(req.ConnectionType)
			So(connectionType, ShouldEqual, config.Auth0SocialConnectionGoogle)
			So(err, ShouldBeNil)
		})

		Convey("should be ok if all is well", func() {
			err := validateRegistrationReq(req, true)
			So(err, ShouldBeNil)
		})

		Convey("should be ok if Company field is empty", func() {
			req.Company = ""
			err := validateRegistrationReq(req, true)
			So(err, ShouldBeNil)
		})

		Convey("should be ok if PhoneNumber field is empty", func() {
			req.PhoneNumber = ""
			err := validateRegistrationReq(req, true)
			So(err, ShouldBeNil)
		})

		Convey("should return error if auth0Token is empty", func() {
			req.Auth0Token = ""

			err := validateRegistrationReq(req, true)
			So(err, ShouldNotBeNil)
		})

		Convey("should return error if Country field is empty", func() {
			req.Country = ""

			err := validateRegistrationReq(req, true)
			So(err, ShouldNotBeNil)
		})
	})

	Convey("Github SSO", t, func() {
		req := &central.RegistrationReq{
			Country:                   "CN",
			FirstName:                 "",
			LastName:                  "",
			Email:                     "",
			Job:                       "",
			Company:                   "TEST",
			PhoneNumber:               "11111111111",
			Password:                  "",
			AcceptService:             true,
			RecaptchaResponse:         "",
			ThirdPartyAccountProvider: "",
			ThirdPartyAccountToken:    "",
			ConnectionType:            central.RegistrationReq_GITHUB,
			Auth0Token:                "xxxx",
		}

		Convey("should return github connection", func() {
			connectionType, err := getAuth0ConnectionType(req.ConnectionType)
			So(connectionType, ShouldEqual, config.Auth0SocialConnectionGithub)
			So(err, ShouldBeNil)
		})

		Convey("should be ok if all is well", func() {
			err := validateRegistrationReq(req, true)
			So(err, ShouldBeNil)
		})

		Convey("should be ok if Company field is empty", func() {
			req.Company = ""
			err := validateRegistrationReq(req, true)
			So(err, ShouldBeNil)
		})

		Convey("should be ok if PhoneNumber field is empty", func() {
			req.PhoneNumber = ""
			err := validateRegistrationReq(req, true)
			So(err, ShouldBeNil)
		})

		Convey("should return error if auth0Token is empty", func() {
			req.Auth0Token = ""

			err := validateRegistrationReq(req, true)
			So(err, ShouldNotBeNil)
		})

		Convey("should return error if Country field is empty", func() {
			req.Country = ""

			err := validateRegistrationReq(req, true)
			So(err, ShouldNotBeNil)
		})
	})

	Convey("Email Password Registration", t, func() {
		req := &central.RegistrationReq{
			Country:                   "CN",
			FirstName:                 "aaa",
			LastName:                  "bbb",
			Email:                     "test@pingcap.com",
			Job:                       "",
			Company:                   "TEST",
			PhoneNumber:               "11111111111",
			Password:                  "xxxxxxxx",
			AcceptService:             true,
			RecaptchaResponse:         "",
			ThirdPartyAccountProvider: "",
			ThirdPartyAccountToken:    "",
			ConnectionType:            central.RegistrationReq_EMAIL,
			Auth0Token:                "xxxx",
		}

		Convey("should return database connection", func() {
			connectionType, err := getAuth0ConnectionType(req.ConnectionType)
			So(connectionType, ShouldEqual, config.Auth0DatabaseConnection)
			So(err, ShouldBeNil)
		})

		Convey("should be ok if all is well", func() {
			err := validateRegistrationReq(req, false)
			So(err, ShouldBeNil)
		})

		Convey("should be ok if Company field is empty", func() {
			req.Company = ""
			err := validateRegistrationReq(req, false)
			So(err, ShouldBeNil)
		})

		Convey("should be ok if PhoneNumber field is empty", func() {
			req.PhoneNumber = ""
			err := validateRegistrationReq(req, false)
			So(err, ShouldBeNil)
		})

		Convey("should return error if Email is empty", func() {
			req.Email = ""

			err := validateRegistrationReq(req, false)
			So(err, ShouldNotBeNil)
		})

		Convey("should return error if FirstName is empty", func() {
			req.FirstName = ""

			err := validateRegistrationReq(req, false)
			So(err, ShouldNotBeNil)
		})

		Convey("should return error if LastName is empty", func() {
			req.LastName = ""

			err := validateRegistrationReq(req, false)
			So(err, ShouldNotBeNil)
		})

		Convey("should return error if Country field is empty", func() {
			req.Country = ""

			err := validateRegistrationReq(req, false)
			So(err, ShouldNotBeNil)
		})
	})
}
