Example #1
0
func TestNilReferencesForwardToStandardBehavior(t *testing.T) {
	t.Parallel()

	thing := new(ThingUnderTest)
	now := time.Now().UTC()
	now2 := thing.CurrentTime()
	thing.Sleep(time.Millisecond * 100)
	now3 := thing.CurrentTime()
	assertions.New(t).So(now, should.HappenWithin, time.Millisecond, now2)
	assertions.New(t).So(now3, should.HappenOnOrAfter, now2.Add(time.Millisecond*100))
}
Example #2
0
func TestActualSleeperInstanceIsUsefulForTesting(t *testing.T) {
	t.Parallel()

	thing := new(ThingUnderTest)
	now := time.Now().UTC()
	thing.sleeper = StayAwake()
	thing.Sleep(time.Hour)
	now2 := thing.CurrentTime()
	assertions.New(t).So(now, should.HappenWithin, time.Millisecond, now2)
	assertions.New(t).So(thing.sleeper.Naps, should.Resemble, []time.Duration{time.Hour})
}
Example #3
0
func TestLoggingWithLoggerCapturesOutput(t *testing.T) {
	out := new(bytes.Buffer)
	log.SetOutput(out)
	log.SetFlags(0)
	defer log.SetOutput(os.Stdout)
	defer log.SetFlags(log.LstdFlags)

	thing := new(ThingUnderTest)
	thing.log = Capture()
	thing.Action()

	assertions.New(t).So(thing.log.Log.String(), should.Equal, "Hello, World!\n")
	assertions.New(t).So(out.Len(), should.Equal, 0)
}
Example #4
0
func TestLoggingWithDiscard(t *testing.T) {
	out := new(bytes.Buffer)
	log.SetOutput(out)
	log.SetFlags(0)
	defer log.SetOutput(os.Stdout)
	defer log.SetFlags(log.LstdFlags)

	thing := new(ThingUnderTest)
	thing.log = Discard()
	thing.Action()

	assertions.New(t).So(thing.log.Log.Len(), should.Equal, 0)
	assertions.New(t).So(out.Len(), should.Equal, 0)
}
Example #5
0
func TestParseAuthServer(t *testing.T) {
	a := assertions.New(t)
	{
		srv, err := parseAuthServer("https://*****:*****@account.thethingsnetwork.org/")
		a.So(err, assertions.ShouldBeNil)
		a.So(srv.url, assertions.ShouldEqual, "https://account.thethingsnetwork.org")
		a.So(srv.username, assertions.ShouldEqual, "user")
		a.So(srv.password, assertions.ShouldEqual, "pass")
	}
	{
		srv, err := parseAuthServer("https://[email protected]/")
		a.So(err, assertions.ShouldBeNil)
		a.So(srv.url, assertions.ShouldEqual, "https://account.thethingsnetwork.org")
		a.So(srv.username, assertions.ShouldEqual, "user")
	}
	{
		srv, err := parseAuthServer("http://account.thethingsnetwork.org/")
		a.So(err, assertions.ShouldBeNil)
		a.So(srv.url, assertions.ShouldEqual, "http://account.thethingsnetwork.org")
	}
	{
		srv, err := parseAuthServer("http://localhost:9090/")
		a.So(err, assertions.ShouldBeNil)
		a.So(srv.url, assertions.ShouldEqual, "http://localhost:9090")
	}
}
Example #6
0
func TestBytes(t *testing.T) {
	a := assertions.New(t)
	a.So(FormatBytes([]byte{0x12, 0x34, 0xcd, 0xef}), assertions.ShouldEqual, "1234CDEF")
	i64, err := ParseBytes("1234CDEF")
	a.So(err, assertions.ShouldBeNil)
	a.So(i64, assertions.ShouldResemble, []byte{0x12, 0x34, 0xcd, 0xef})
}
Example #7
0
func TestUint64(t *testing.T) {
	a := assertions.New(t)
	a.So(FormatUint64(123456), assertions.ShouldEqual, "123456")
	i64, err := ParseUint64("123456")
	a.So(err, assertions.ShouldBeNil)
	a.So(i64, assertions.ShouldEqual, 123456)
}
Example #8
0
func TestInt64(t *testing.T) {
	a := assertions.New(t)
	a.So(FormatInt64(-123456), assertions.ShouldEqual, "-123456")
	i64, err := ParseInt64("-123456")
	a.So(err, assertions.ShouldBeNil)
	a.So(i64, assertions.ShouldEqual, -123456)
}
Example #9
0
func TestFloat64(t *testing.T) {
	a := assertions.New(t)
	a.So(FormatFloat64(123.456), assertions.ShouldEqual, "123.456")
	f64, err := ParseFloat64("123.456")
	a.So(err, assertions.ShouldBeNil)
	a.So(f64, assertions.ShouldEqual, 123.456)
}
Example #10
0
func TestExchangeAppKeyForToken(t *testing.T) {
	for _, env := range strings.Split("ACCOUNT_SERVER_PROTO ACCOUNT_SERVER_USERNAME ACCOUNT_SERVER_PASSWORD ACCOUNT_SERVER_URL APP_ID APP_TOKEN", " ") {
		if os.Getenv(env) == "" {
			t.Skipf("Skipping auth server test: %s configured", env)
		}
	}

	a := assertions.New(t)
	c := new(Component)
	c.Config.KeyDir = os.TempDir()
	c.Config.AuthServers = map[string]string{
		"ttn-account-preview": fmt.Sprintf("%s://%s:%s@%s",
			os.Getenv("ACCOUNT_SERVER_PROTO"),
			os.Getenv("ACCOUNT_SERVER_USERNAME"),
			os.Getenv("ACCOUNT_SERVER_PASSWORD"),
			os.Getenv("ACCOUNT_SERVER_URL"),
		),
	}
	c.initAuthServers()

	{
		token, err := c.ExchangeAppKeyForToken(os.Getenv("APP_ID"), "ttn-account-preview."+os.Getenv("APP_TOKEN"))
		a.So(err, assertions.ShouldBeNil)
		a.So(token, assertions.ShouldNotBeEmpty)
	}

	{
		token, err := c.ExchangeAppKeyForToken(os.Getenv("APP_ID"), os.Getenv("APP_TOKEN"))
		a.So(err, assertions.ShouldBeNil)
		a.So(token, assertions.ShouldNotBeEmpty)
	}
}
Example #11
0
func TestLogCallsAreCounted(t *testing.T) {
	thing := new(ThingUnderTest)
	thing.log = Capture()
	for x := 0; x < 10; x++ {
		thing.Action()
	}
	assertions.New(t).So(thing.log.Calls, should.Equal, 10)
}
Example #12
0
func TestActualClockInstanceIsUsefulForTesting(t *testing.T) {
	t.Parallel()

	thing := new(ThingUnderTest)
	now := time.Now().UTC()
	thing.clock = Freeze(now)
	now2 := thing.CurrentTime()
	assertions.New(t).So(now, should.Resemble, now2)
}
Example #13
0
func TestBool(t *testing.T) {
	a := assertions.New(t)
	a.So(FormatBool(true), assertions.ShouldEqual, "true")
	b, err := ParseBool("true")
	a.So(err, assertions.ShouldBeNil)
	a.So(b, assertions.ShouldEqual, true)
	a.So(FormatBool(false), assertions.ShouldEqual, "false")
	b, err = ParseBool("false")
	a.So(err, assertions.ShouldBeNil)
	a.So(b, assertions.ShouldEqual, false)
}
Example #14
0
func TestSimple(t *testing.T) {
	a := assertions.New(t)

	diff, equal := Diff("hey", "there")
	a.So(equal, should.BeFalse)
	a.So(FormatTest(diff), should.Equal, `string: got: "there", expected: "hey"`)

	diff, equal = Diff("hey", "hey")
	a.So(equal, should.BeTrue)
	a.So(FormatTest(diff), should.Equal, ``)
}
Example #15
0
func TestLoggingWithNilReferenceProducesTraditionalBehavior(t *testing.T) {
	out := new(bytes.Buffer)
	log.SetOutput(out)
	log.SetFlags(0)
	defer log.SetOutput(os.Stdout)
	defer log.SetFlags(log.LstdFlags)

	thing := new(ThingUnderTest)
	thing.Action()

	assertions.New(t).So(out.String(), should.Equal, "Hello, World!\n")
}
Example #16
0
func TestCyclicNatureOfFrozenClock(t *testing.T) {
	t.Parallel()

	thing := new(ThingUnderTest)
	now1 := time.Now()
	now2 := now1.Add(time.Second)
	now3 := now2.Add(time.Second)

	thing.clock = Freeze(now1, now2, now3)

	now1a := thing.CurrentTime()
	now2a := thing.CurrentTime()
	now3a := thing.CurrentTime()

	assertions.New(t).So(now1, should.Resemble, now1a)
	assertions.New(t).So(now2, should.Resemble, now2a)
	assertions.New(t).So(now3, should.Resemble, now3a)

	now1b := thing.CurrentTime()
	now2b := thing.CurrentTime()
	now3b := thing.CurrentTime()

	assertions.New(t).So(now1, should.Resemble, now1b)
	assertions.New(t).So(now2, should.Resemble, now2b)
	assertions.New(t).So(now3, should.Resemble, now3b)
}
Example #17
0
func TestValidateTTNAuthContext(t *testing.T) {
	for _, env := range strings.Split("ACCOUNT_SERVER_PROTO ACCOUNT_SERVER_URL", " ") {
		if os.Getenv(env) == "" {
			t.Skipf("Skipping auth server test: %s configured", env)
		}
	}
	accountServer := fmt.Sprintf("%s://%s",
		os.Getenv("ACCOUNT_SERVER_PROTO"),
		os.Getenv("ACCOUNT_SERVER_URL"),
	)

	a := assertions.New(t)
	c := new(Component)
	c.Config.KeyDir = os.TempDir()
	c.Config.AuthServers = map[string]string{
		"ttn-account-preview": accountServer,
	}
	err := c.initAuthServers()
	a.So(err, assertions.ShouldBeNil)

	{
		ctx := context.Background()
		_, err = c.ValidateTTNAuthContext(ctx)
		a.So(err, assertions.ShouldNotBeNil)
	}

	{
		md := metadata.Pairs()
		ctx := metadata.NewContext(context.Background(), md)
		_, err = c.ValidateTTNAuthContext(ctx)
		a.So(err, assertions.ShouldNotBeNil)
	}

	{
		md := metadata.Pairs(
			"id", "dev",
		)
		ctx := metadata.NewContext(context.Background(), md)
		_, err = c.ValidateTTNAuthContext(ctx)
		a.So(err, assertions.ShouldNotBeNil)
	}

	{
		md := metadata.Pairs(
			"id", "dev",
			"token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJ0dG4tYWNjb3VudC1wcmV2aWV3Iiwic3ViIjoiZGV2IiwidHlwZSI6InJvdXRlciIsImlhdCI6MTQ3NjQzOTQzOH0.Duz-E5aMYEPY_Nf5Pky7Qmjbs1dMp9PN9nMqbSzoU079b8TPL4DH2SKcRHrrMqieB3yhJb3YaQBfY6dKWfgVz8BmTeKlGXfFrqEj91y30J7r9_VsHRzgDMJedlqXryvf0S_yD27TsJ7TMbGYyE00T4tAX3Uf6wQZDhdyHNGtdf4jtoAjzOxVAodNtXZp26LR7fFk56UstBxOxztBMzyzmAdiTG4lSyEqq7zsuJcFjmHB9MfEoD4ZT-iTRL1ohFjGuj2HN49oPyYlZAVPP7QajLyNsLnv-nDqXE_QecOjAcEq4PLNJ3DpXtX-lo8I_F1eV9yQnDdQQi4EUvxmxZWeBA",
		)
		ctx := metadata.NewContext(context.Background(), md)
		_, err = c.ValidateTTNAuthContext(ctx)
		a.So(err, assertions.ShouldBeNil)
	}
}
Example #18
0
func TestAccessKeysRights(t *testing.T) {
	a := s.New(t)
	c := AccessKey{
		Name: "name",
		Key:  "123456",
		Rights: []Right{
			Right("right"),
		},
	}

	a.So(c.HasRight(Right("right")), s.ShouldBeTrue)
	a.So(c.HasRight(Right("foo")), s.ShouldBeFalse)
}
Example #19
0
func TestInitAuthServers(t *testing.T) {
	for _, env := range strings.Split("ACCOUNT_SERVER_PROTO ACCOUNT_SERVER_USERNAME ACCOUNT_SERVER_PASSWORD ACCOUNT_SERVER_URL", " ") {
		if os.Getenv(env) == "" {
			t.Skipf("Skipping auth server test: %s configured", env)
		}
	}

	a := assertions.New(t)
	c := new(Component)
	c.Config.KeyDir = os.TempDir()
	c.Ctx = GetLogger(t, "TestInitAuthServers")
	c.Config.AuthServers = map[string]string{
		"ttn": fmt.Sprintf("%s://%s",
			os.Getenv("ACCOUNT_SERVER_PROTO"),
			os.Getenv("ACCOUNT_SERVER_URL"),
		),
		"ttn-user": fmt.Sprintf("%s://%s@%s",
			os.Getenv("ACCOUNT_SERVER_PROTO"),
			os.Getenv("ACCOUNT_SERVER_USERNAME"),
			os.Getenv("ACCOUNT_SERVER_URL"),
		),
		"ttn-user-pass": fmt.Sprintf("%s://%s:%s@%s",
			os.Getenv("ACCOUNT_SERVER_PROTO"),
			os.Getenv("ACCOUNT_SERVER_USERNAME"),
			os.Getenv("ACCOUNT_SERVER_PASSWORD"),
			os.Getenv("ACCOUNT_SERVER_URL"),
		),
	}
	err := c.initAuthServers()
	a.So(err, assertions.ShouldBeNil)

	{
		k, err := c.TokenKeyProvider.Get("ttn", true)
		a.So(err, assertions.ShouldBeNil)
		a.So(k.Algorithm, assertions.ShouldEqual, "RS256")
	}

	{
		k, err := c.TokenKeyProvider.Get("ttn-user", true)
		a.So(err, assertions.ShouldBeNil)
		a.So(k.Algorithm, assertions.ShouldEqual, "RS256")
	}

	{
		k, err := c.TokenKeyProvider.Get("ttn-user-pass", true)
		a.So(err, assertions.ShouldBeNil)
		a.So(k.Algorithm, assertions.ShouldEqual, "RS256")
	}

	a.So(c.UpdateTokenKey(), assertions.ShouldBeNil)
}
Example #20
0
func TestInit(t *testing.T) {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	tmpDir := fmt.Sprintf("%s/%d", os.TempDir(), r.Int63())
	os.Mkdir(tmpDir, 755)
	defer os.Remove(tmpDir)

	a := assertions.New(t)
	c := new(Component)
	c.Identity = new(discovery.Announcement)
	c.Config.KeyDir = tmpDir

	security.GenerateKeypair(tmpDir)

	a.So(c.InitAuth(), assertions.ShouldBeNil)
}
Example #21
0
func TestStructDiffFormatTest2to1(t *testing.T) {
	a := assertions.New(t)

	diff, equal := Diff(teststruct2, teststruct1)
	a.So(equal, should.BeFalse)
	a.So(FormatTest(diff), should.Equal, `idiff.Data.List3[0]: added: "removed"
idiff.Data.Map["key1"]: added: "value2"
idiff.Data.List2[0]: removed: "added"
idiff.Data.Map["key2"]: removed: "value2"
idiff.Data.ID: got: "file1", expected: "file2"
idiff.Data.Value: got: "value1", expected: "value2"
idiff.Data.List1[0]: got: "hey", expected: "changed1"
idiff.Data.List1[1]: got: "there", expected: "changed2"
idiff.Data.Popup.MenuItem[0].Value: got: "New", expected: "Newier"
idiff.Data.Popup.MenuItem[1].Value: got: "Open", expected: "Open it"
idiff.Data.Popup.MenuItem[2].Value: got: "Close", expected: "Close it"
idiff.Data.Func: got: not nil, expected: nil
idiff.Data.Recursive.ID: got: "rec1", expected: "rec2"`)
}
Example #22
0
func TestGetAndVerifyContext(t *testing.T) {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	tmpDir := fmt.Sprintf("%s/%d", os.TempDir(), r.Int63())
	os.Mkdir(tmpDir, 755)
	defer os.Remove(tmpDir)

	a := assertions.New(t)
	c := new(Component)

	c.Identity = new(discovery.Announcement)

	c.Config.KeyDir = tmpDir
	security.GenerateKeypair(tmpDir)
	c.initKeyPair()

	{
		ctx := c.GetContext("")
		_, err := c.ValidateNetworkContext(ctx)
		a.So(err, assertions.ShouldNotBeNil)
	}

	c.Identity.Id = "test-context"
	{
		ctx := c.GetContext("")
		_, err := c.ValidateNetworkContext(ctx)
		a.So(err, assertions.ShouldNotBeNil)
	}

	c.Identity.ServiceName = "test-service"

	ctrl := gomock.NewController(t)
	discoveryClient := discovery.NewMockClient(ctrl)
	c.Discovery = discoveryClient

	discoveryClient.EXPECT().Get("test-service", "test-context").Return(c.Identity, nil)

	ctx := c.GetContext("")
	_, err := c.ValidateNetworkContext(ctx)
	a.So(err, assertions.ShouldBeNil)

}
Example #23
0
func TestRightString(t *testing.T) {
	a := s.New(t)

	right := Right("foo")
	a.So(right.String(), s.ShouldEqual, "foo")
}