Example #1
0
func TestStoreCodeFromClaimFullToken(t *testing.T) {
	s := store.MustNewStore(
		&store.TableStore{StoreID: 1, Code: dbr.NewNullString("de"), WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true},
		&store.TableWebsite{WebsiteID: 1, Code: dbr.NewNullString("admin"), Name: dbr.NewNullString("Admin"), SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NewNullBool(false)},
		&store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0},
	)
	token := jwt.New(jwt.SigningMethodHS256)
	s.AddClaim(token.Claims)

	so, err := store.CodeFromClaim(token.Claims)
	assert.NoError(t, err)
	assert.EqualValues(t, "de", so.StoreCode())

	so, err = store.CodeFromClaim(nil)
	assert.EqualError(t, store.ErrStoreNotFound, err.Error())
	assert.Nil(t, so.Website)
	assert.Nil(t, so.Group)
	assert.Nil(t, so.Store)

	token2 := jwt.New(jwt.SigningMethodHS256)
	token2.Claims[store.ParamName] = "Invalid Cod€"
	so, err = store.CodeFromClaim(token2.Claims)
	assert.EqualError(t, store.ErrStoreCodeInvalid, err.Error())
	assert.Nil(t, so.Website)
	assert.Nil(t, so.Group)
	assert.Nil(t, so.Store)
}
Example #2
0
func TestStoreCodeFromClaimNoToken(t *testing.T) {
	tests := []struct {
		token     map[string]interface{}
		wantErr   error
		wantScope scope.Scope
		wantCode  string
		wantID    int64
	}{
		{
			map[string]interface{}{},
			store.ErrStoreNotFound,
			scope.DefaultID,
			"",
			0,
		},
		{
			map[string]interface{}{store.ParamName: "dede"},
			nil,
			scope.StoreID,
			"dede",
			scope.UnavailableStoreID,
		},
		{
			map[string]interface{}{store.ParamName: "de'de"},
			store.ErrStoreCodeInvalid,
			scope.DefaultID,
			"",
			scope.UnavailableStoreID,
		},
		{
			map[string]interface{}{store.ParamName: 1},
			store.ErrStoreNotFound,
			scope.DefaultID,
			"",
			scope.UnavailableStoreID,
		},
	}
	for i, test := range tests {
		so, err := store.CodeFromClaim(test.token)
		testStoreCodeFrom(t, i, err, test.wantErr, so, test.wantScope, test.wantCode, test.wantID)
	}
}