)

var _ = Describe("Onboard Gateway App", func() {
	var (
		app  common.App
		addr common.GatewayAddress
		t    string
	)
	BeforeEach(func() {
		app = tConfig.Apps["master"]
		addr = tConfig.GatewayAddress
	})

	Context("If provide with valid local token", func() {
		JustBeforeEach(func() {
			token, _, err := util.LocalAuth(addr, app, "admin_user", "admin_pass")
			Expect(err).To(Succeed())
			Expect(token).NotTo(Equal(""))
			t = token
		})
		It("Should success", func() {
			url := fmt.Sprintf("http://%s:%d/gateway-app/gateway/onboarding", addr.Host, addr.Port)
			req, err := util.NewRequest("POST", url, nil)
			Expect(err).To(Succeed())
			req.Header.Add("Authorization", "Bearer "+t)
			body, status, err := util.ExecuteRequest(req)
			Expect(err).To(Succeed())
			Expect(status).To(Equal(200))
			var v interface{}
			err = json.Unmarshal(body, &v)
			Expect(err).To(Succeed())
	"github.com/KiiPlatform/gateway-agent/tests/integration/common"
	"github.com/KiiPlatform/gateway-agent/tests/integration/util"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("BasicAuth", func() {
	var app common.App
	BeforeEach(func() {
		app = tConfig.Apps["master"]
	})

	Context("When username and password is valid", func() {
		It("Access Token should be in response body", func() {
			t, status, err := util.LocalAuth(tConfig.GatewayAddress, app, "admin_user", "admin_pass")
			Expect(err).ShouldNot(HaveOccurred())
			Expect(t).ShouldNot(Equal(""))
			Expect(status).Should(Equal(200))
		})
	})

	Context("When username/password is invalid(INVALID_GRANT)", func() {
		It("Should fail with INVALID_GRANT", func() {
			_, status, err := util.LocalAuth(tConfig.GatewayAddress, app, "admin", "admin_pass")
			Expect(status).Should(Equal(401))
			Expect(err.Error()).Should(Equal(`{"errorCode":"INVALID_GRANT","message":"Can't authenticate with the given username and password"}`))
		})
	})

	Context("When appID/appKey is invalid(INVALID_INPUT_DATA)", func() {
			By("Delete endnode after test")
			// Delete Endnode
			Expect(util.DeleteThing(app, user, enID)).To(Succeed())
		})

		It("Should success", func() {
			var (
				newVid string
				t      string
			)
			thingProperties := `{"_thingType":"LED"}`
			endnodeState := `{"power":true}`

			By("Get local token")
			t, _, err := util.LocalAuth(tConfig.GatewayAddress, app, "admin_user", "admin_pass")
			Expect(err).To(Succeed())
			Expect(t).ShouldNot(Equal(""))

			By("Request Onboard app")
			gwID, _, err := util.OnboardApp(tConfig.GatewayAddress, app, t)
			Expect(err).To(Succeed())
			Expect(gwID).ShouldNot(Equal(""))

			By("Onboard new end-node to gateway-agent")
			newVid = fmt.Sprintf("vid-%d", time.Now().UnixNano()/int64(time.Millisecond))
			err = util.OnboardEndnode(client, app, newVid)
			Expect(err).To(Succeed())

			By("Report connect with thing properties before onboard")
			err = util.ReportConnectStatus(client, app, newVid, thingProperties)
	AfterEach(func() {
		if client.IsConnected() {
			client.Disconnect(0)
		}
		if client1.IsConnected() {
			client1.Disconnect(0)
		}

		// remove endnodes
		Expect(util.DeleteThing(master, mUser, mEnID)).To(Succeed())
		Expect(util.DeleteThing(en, eUser, eEnID)).To(Succeed())
	})

	It("Should Success", func() {
		By("Get local token of master app")
		t, _, err := util.LocalAuth(addr, master, "admin_user", "admin_pass")
		Expect(err).To(Succeed())
		Expect(t).NotTo(Equal(""))

		By("Onboard gateway")
		_, _, err = util.OnboardMasterApp(tConfig.GatewayAddress, t)
		Expect(err).NotTo(Succeed())

		By("Restore gateway")
		url := fmt.Sprintf("http://%s:%d/gateway-app/gateway/restore", addr.Host, addr.Port)
		req, err := util.NewRequest("POST", url, nil)
		Expect(err).To(Succeed())
		req.Header.Add("Authorization", "Bearer "+t)
		_, status, err := util.ExecuteRequest(req)
		Expect(status).To(Equal(204))
		Expect(err).To(Succeed())