Ejemplo n.º 1
0
			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() {
		It("Should fail with INVALID_INPUT_DATA", func() {
			username := "******"
			password := "******"
			url := fmt.Sprintf("http://%s:%d/%s/token", tConfig.GatewayAddress.Host, tConfig.GatewayAddress.Port, app.Site)
			payload := map[string]string{
				"username": username,
				"password": password,
			}
			req, _ := util.NewRequest("POST", url, payload)
			_, status, err := util.ExecuteRequest(req)
			Expect(status).Should(Equal(400))
			Expect(err.Error()).To(Equal(`{"errorCode":"INVALID_INPUT_DATA","message":"Missing authorization header"}`))
		})
	})
})
		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())
			Expect(v).To(HaveKey("thingID"))
		})
	})
})
			r := presp.CommandID == command.CommandID
			lock.Unlock()
			return r
		}).Should(Equal(true))

		By("Replace endnode to Cloud")
		newVid2 := fmt.Sprintf("vid-%d", time.Now().UnixNano()/int64(time.Millisecond))
		Expect(util.UpdateVID(app, user, enID, newVid2, "pass")).To(Succeed())

		By("Replace endnode locally")
		url := fmt.Sprintf("http://%s:%d/%s/apps/%s/gateway/end-nodes/THING_ID:%s",
			addr.Host, addr.Port, app.Site, app.ID, enID)
		payload := map[string]string{
			"vendorThingID": newVid2,
		}
		req, err := util.NewRequest("PUT", url, payload)
		Expect(err).To(Succeed())
		req.Header.Add("authorization", "Bearer "+t)
		_, status, err := util.ExecuteRequest(req)
		Expect(err).To(Succeed())
		Expect(status).To(Equal(204))

		By("unsubscribe topic for old vid")
		token := client.Unsubscribe(cTopic)
		Eventually(func() error {
			return token.Error()
		}).Should(Succeed())

		By("Subscribe to receiving commands for new vid")
		// reset command
		command = common.Command{}