Ejemplo n.º 1
0
func TestNewManagerStore(t *testing.T) {
	assert.True(t, managerStoreSimpleTest.IsCacheEmpty())
	for j := 0; j < 3; j++ {
		s, err := managerStoreSimpleTest.Store(config.ScopeCode("notNil"))
		assert.NoError(t, err)
		assert.NotNil(t, s)
		assert.EqualValues(t, "de", s.Data.Code.String)
	}
	assert.False(t, managerStoreSimpleTest.IsCacheEmpty())
	managerStoreSimpleTest.ClearCache()
	assert.True(t, managerStoreSimpleTest.IsCacheEmpty())

	tests := []struct {
		have    config.ScopeIDer
		wantErr error
	}{
		{config.ScopeCode("nilSlices"), store.ErrStoreNotFound},
		{config.ScopeID(2), store.ErrStoreNotFound},
		{nil, store.ErrAppStoreNotSet},
	}

	managerEmpty := getTestManager()
	for _, test := range tests {
		s, err := managerEmpty.Store(test.have)
		assert.Nil(t, s)
		assert.EqualError(t, test.wantErr, err.Error())
	}
	assert.True(t, managerStoreSimpleTest.IsCacheEmpty())
}
Ejemplo n.º 2
0
func TestNewManagerGetRequestStore_ScopeWebsite(t *testing.T) {
	testCode := config.ScopeID(1)
	testScope := config.ScopeWebsiteID

	if haveStore, haveErr := storeManagerRequestStore.GetRequestStore(config.ScopeID(1), testScope); haveErr == nil {
		t.Error("appStore should not be set!")
		t.Fail()
	} else {
		assert.Nil(t, haveStore)
		assert.EqualError(t, store.ErrAppStoreNotSet, haveErr.Error())
	}

	assert.EqualError(t, store.ErrUnsupportedScopeGroup, storeManagerRequestStore.Init(config.ScopeID(123), config.ScopeDefaultID).Error())
	assert.EqualError(t, store.ErrWebsiteNotFound, storeManagerRequestStore.Init(config.ScopeID(123), testScope).Error())
	if err := storeManagerRequestStore.Init(testCode, testScope); err != nil {
		t.Error(err)
		t.Fail()
	}
	assert.EqualError(t, store.ErrAppStoreSet, storeManagerRequestStore.Init(testCode, testScope).Error())

	if s, err := storeManagerRequestStore.Store(); err == nil {
		assert.EqualValues(t, "at", s.Data.Code.String)
	} else {
		assert.EqualError(t, err, store.ErrStoreNotFound.Error())
		t.Fail()
	}

	if w, err := storeManagerRequestStore.Website(); err == nil {
		assert.EqualValues(t, "euro", w.Data.Code.String)
	} else {
		assert.EqualError(t, err, store.ErrStoreNotFound.Error())
		t.Fail()
	}

	tests := []testNewManagerGetRequestStore{
		{config.ScopeID(232), "", store.ErrStoreNotFound},
		{nil, "", store.ErrStoreNotFound},
		{config.ScopeCode("\U0001f631"), "", store.ErrStoreNotFound},

		{config.ScopeID(6), "nz", store.ErrStoreChangeNotAllowed},
		{config.ScopeCode("ch"), "", store.ErrStoreNotActive},

		{config.ScopeCode("de"), "de", nil},
		{config.ScopeID(2), "at", nil},

		{config.ScopeID(2), "at", nil},
		{config.ScopeCode("au"), "au", store.ErrStoreChangeNotAllowed},
		{config.ScopeCode("ch"), "", store.ErrStoreNotActive},
	}
	runNewManagerGetRequestStore(t, testScope, tests)
}
Ejemplo n.º 3
0
// InitByRequest returns a new Store read from a cookie or HTTP request param.
// The internal appStore must be set before hand.
// 1. check cookie store, always a string and the store code
// 2. check for ___store variable, always a string and the store code
// 3. May return nil,nil if nothing is set.
// This function must be used within an HTTP handler.
// The returned new Store must be used in the HTTP context and overrides the appStore.
// @see \Magento\Store\Model\StorageFactory::_reinitStores
func (sm *Manager) InitByRequest(res http.ResponseWriter, req *http.Request, scopeType config.ScopeGroup) (*Store, error) {
	if sm.appStore == nil {
		// that means you must call Init() before executing this function.
		return nil, ErrAppStoreNotSet
	}

	var reqStore *Store
	if keks := GetCodeFromCookie(req); keks != nil {
		reqStore, _ = sm.GetRequestStore(keks, scopeType) // ignore errors
	}

	if reqStoreCode := req.URL.Query().Get(HTTPRequestParamStore); reqStoreCode != "" {
		var err error
		// @todo reqStoreCode if number ... cast to int64 because then group id if ScopeGroup is group.
		if reqStore, err = sm.GetRequestStore(config.ScopeCode(reqStoreCode), scopeType); err != nil {
			return nil, errgo.Mask(err)
		}
		// also delete and re-set a new cookie
		if reqStore != nil && reqStore.Data().Code.String == reqStoreCode {
			wds, err := reqStore.Website().DefaultStore()
			if err != nil {
				return nil, errgo.Mask(err)
			}
			if wds.Data().Code.String == reqStoreCode {
				reqStore.DeleteCookie(res) // cookie not needed anymore
			} else {
				reqStore.SetCookie(res) // make sure we force set the new store
			}
		}
	}
	return reqStore, nil // can be nil,nil
}
Ejemplo n.º 4
0
func TestNewManagerReInit(t *testing.T) {
	numCPU := runtime.NumCPU()
	prevCPU := runtime.GOMAXPROCS(numCPU)
	t.Logf("GOMAXPROCS was: %d now: %d", prevCPU, numCPU)
	defer runtime.GOMAXPROCS(prevCPU)

	// quick implement, use mock of dbr.SessionRunner and remove connection
	db := csdb.MustConnectTest()
	defer db.Close()
	dbrSess := dbr.NewConnection(db, nil).NewSession(nil)

	storeManager := store.NewManager(store.NewStorageOption(nil /* trick it*/))
	if err := storeManager.ReInit(dbrSess); err != nil {
		t.Fatal(err)
	}

	tests := []struct {
		have    config.ScopeIDer
		wantErr error
	}{
		{config.ScopeCode("de"), nil},
		{config.ScopeCode("cz"), store.ErrStoreNotFound},
		{config.ScopeCode("de"), nil},
		{config.ScopeID(1), nil},
		{config.ScopeID(100), store.ErrStoreNotFound},
		{mockIDCode{1, "de"}, nil},
		{mockIDCode{2, "cz"}, store.ErrStoreNotFound},
		{mockIDCode{2, ""}, nil},
		{nil, store.ErrAppStoreNotSet}, // if set returns default store
	}

	for _, test := range tests {
		s, err := storeManager.Store(test.have)
		if test.wantErr == nil {
			assert.NoError(t, err, "For test: %#v", test)
			assert.NotNil(t, s)
			//			assert.NotEmpty(t, s.Data().Code.String, "%#v", s.Data())
		} else {
			assert.Error(t, err, "For test: %#v", test)
			assert.EqualError(t, test.wantErr, err.Error(), "For test: %#v", test)
			assert.Nil(t, s)
		}
	}
	assert.False(t, storeManager.IsCacheEmpty())
	storeManager.ClearCache()
	assert.True(t, storeManager.IsCacheEmpty())
}
Ejemplo n.º 5
0
// GetCookie returns from a Request the value of the store cookie or nil.
func GetCodeFromCookie(req *http.Request) config.ScopeIDer {
	if req == nil {
		return nil
	}
	if keks, err := req.Cookie(CookieName); nil == err && nil == ValidateStoreCode(keks.Value) {
		return config.ScopeCode(keks.Value)
	}
	return nil
}
Ejemplo n.º 6
0
func TestStorageWebsite(t *testing.T) {

	tests := []struct {
		have      config.ScopeIDer
		err       error
		wantWCode string
	}{
		{nil, store.ErrWebsiteNotFound, ""},
		{config.ScopeID(2015), store.ErrWebsiteNotFound, ""},
		{config.ScopeID(1), nil, "euro"},
		{config.ScopeCode("asia"), store.ErrWebsiteNotFound, ""},
		{config.ScopeCode("oz"), nil, "oz"},
		{mockIDCode{1, "oz"}, nil, "oz"},
		{mockIDCode{1, "ozzz"}, store.ErrWebsiteNotFound, ""},
	}
	for _, test := range tests {
		w, err := testStorage.Website(test.have)
		if test.err != nil {
			assert.Nil(t, w)
			assert.EqualError(t, test.err, err.Error())
		} else {
			assert.NotNil(t, w, "Test: %#v", test)
			assert.NoError(t, err, "Test: %#v", test)
			assert.Equal(t, test.wantWCode, w.Data().Code.String, "Test: %#v", test)
		}
	}

	w, err := testStorage.Website(config.ScopeCode("euro"))
	assert.NoError(t, err)
	assert.NotNil(t, w)

	dGroup, err := w.DefaultGroup()
	assert.NoError(t, err)
	assert.EqualValues(t, "DACH Group", dGroup.Data().Name)

	groups, err := w.Groups()
	assert.NoError(t, err)
	assert.EqualValues(t, utils.Int64Slice{1, 2}, groups.IDs())

	stores, err := w.Stores()
	assert.NoError(t, err)
	assert.EqualValues(t, utils.StringSlice{"de", "uk", "at", "ch"}, stores.Codes())
}
Ejemplo n.º 7
0
// GetClaim returns a valid store code from a JSON web token or nil
func GetCodeFromClaim(t *jwt.Token) config.ScopeIDer {
	if t == nil {
		return nil
	}
	c, ok := t.Claims[CookieName]
	if cs, okcs := c.(string); okcs && ok && nil == ValidateStoreCode(cs) {
		return config.ScopeCode(cs)
	}
	return nil
}
Ejemplo n.º 8
0
func TestNewManagerWebsiteInit(t *testing.T) {

	err := getTestManager(func(ms *mockStorage) {
		ms.w = func() (*store.Website, error) {
			return store.NewWebsite(
				&store.TableWebsite{WebsiteID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "euro", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Europe", Valid: true}}, SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: true, Valid: true}}},
			), nil
		}
	}).Init(config.ScopeCode("euro"), config.ScopeWebsiteID)
	assert.EqualError(t, store.ErrWebsiteDefaultGroupNotFound, err.Error())

	managerWebsite := getTestManager(func(ms *mockStorage) {
		ms.w = func() (*store.Website, error) {
			return store.NewWebsite(
				&store.TableWebsite{WebsiteID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "euro", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Europe", Valid: true}}, SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: true, Valid: true}}},
			).SetGroupsStores(
				store.TableGroupSlice{
					&store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2},
				},
				store.TableStoreSlice{
					&store.TableStore{StoreID: 0, Code: dbr.NullString{NullString: sql.NullString{String: "admin", Valid: true}}, WebsiteID: 0, GroupID: 0, Name: "Admin", SortOrder: 0, IsActive: true},
					&store.TableStore{StoreID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "de", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true},
					&store.TableStore{StoreID: 2, Code: dbr.NullString{NullString: sql.NullString{String: "at", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Österreich", SortOrder: 20, IsActive: true},
					&store.TableStore{StoreID: 3, Code: dbr.NullString{NullString: sql.NullString{String: "ch", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Schweiz", SortOrder: 30, IsActive: true},
				},
			), nil
		}
	})
	w1, err := managerWebsite.Website()
	assert.EqualError(t, store.ErrAppStoreNotSet, err.Error())
	assert.Nil(t, w1)

	err = managerWebsite.Init(config.ScopeCode("euro"), config.ScopeWebsiteID)
	assert.NoError(t, err)

	w2, err := managerWebsite.Website()
	assert.NoError(t, err)
	assert.EqualValues(t, "euro", w2.Data.Code.String)

	err3 := getTestManager(func(ms *mockStorage) {}).Init(config.ScopeCode("euronen"), config.ScopeWebsiteID)
	assert.Error(t, err3, "config.ScopeCode(euro), config.ScopeWebsite: %#v => %s", err3, err3)
	assert.EqualError(t, store.ErrWebsiteNotFound, err3.Error())
}
Ejemplo n.º 9
0
func TestStorageStore(t *testing.T) {

	tests := []struct {
		have     config.ScopeIDer
		err      error
		wantCode string
	}{
		{nil, store.ErrStoreNotFound, ""},
		{config.ScopeID(2015), store.ErrStoreNotFound, ""},
		{config.ScopeID(1), nil, "de"},
		{config.ScopeCode("asia"), store.ErrStoreNotFound, ""},
		{config.ScopeCode("nz"), nil, "nz"},
		{mockIDCode{4, "nz"}, nil, "nz"},
		{mockIDCode{4, "auuuuu"}, store.ErrStoreNotFound, ""},
	}
	for _, test := range tests {
		s, err := testStorage.Store(test.have)
		if test.err != nil {
			assert.Nil(t, s, "%#v", test)
			assert.EqualError(t, test.err, err.Error())
		} else {
			assert.NotNil(t, s, "%#v", test)
			assert.NoError(t, err, "%#v", test)
			assert.Equal(t, test.wantCode, s.Data().Code.String)
		}
	}

	s, err := testStorage.Store(config.ScopeCode("at"))
	assert.NoError(t, err)
	assert.NotNil(t, s)

	assert.EqualValues(t, "DACH Group", s.Group().Data().Name)

	website := s.Website()
	assert.EqualValues(t, "euro", website.Data().Code.String)
	wg, err := website.DefaultGroup()
	assert.NotNil(t, wg)
	assert.EqualValues(t, "DACH Group", wg.Data().Name)
	wgs, err := wg.DefaultStore()
	assert.NoError(t, err)
	assert.EqualValues(t, "at", wgs.Data().Code.String)
}
Ejemplo n.º 10
0
// BenchmarkManagerGetStore	 5000000	       355 ns/op	      24 B/op	       2 allocs/op
func BenchmarkManagerGetStore(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var err error
		benchmarkManagerStore, err = managerStoreSimpleTest.Store(config.ScopeCode("de"))
		if err != nil {
			b.Error(err)
		}
		if benchmarkManagerStore == nil {
			b.Error("benchmarkManagerStore is nil")
		}
	}
}
Ejemplo n.º 11
0
func TestStorageStoreErrors(t *testing.T) {

	var nsw = store.NewStorage(
		store.SetStorageWebsites(),
		store.SetStorageGroups(),
		store.SetStorageStores(
			&store.TableStore{StoreID: 4, Code: dbr.NullString{NullString: sql.NullString{String: "au", Valid: true}}, WebsiteID: 2, GroupID: 3, Name: "Australia", SortOrder: 10, IsActive: true},
			&store.TableStore{StoreID: 6, Code: dbr.NullString{NullString: sql.NullString{String: "nz", Valid: true}}, WebsiteID: 2, GroupID: 3, Name: "Kiwi", SortOrder: 30, IsActive: true},
		),
	)
	stw, err := nsw.Store(config.ScopeCode("nz"))
	assert.Nil(t, stw)
	assert.EqualError(t, store.ErrWebsiteNotFound, err.Error())

	stws, err := nsw.Stores()
	assert.Nil(t, stws)
	assert.EqualError(t, store.ErrWebsiteNotFound, err.Error())

	var nsg = store.NewStorage(
		store.SetStorageWebsites(
			&store.TableWebsite{WebsiteID: 2, Code: dbr.NullString{NullString: sql.NullString{String: "oz", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "OZ", Valid: true}}, SortOrder: 20, DefaultGroupID: 3, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}},
		),
		store.SetStorageGroups(
			&store.TableGroup{GroupID: 13, WebsiteID: 12, Name: "Australia", RootCategoryID: 2, DefaultStoreID: 4},
		),
		store.SetStorageStores(
			&store.TableStore{StoreID: 4, Code: dbr.NullString{NullString: sql.NullString{String: "au", Valid: true}}, WebsiteID: 2, GroupID: 3, Name: "Australia", SortOrder: 10, IsActive: true},
			&store.TableStore{StoreID: 6, Code: dbr.NullString{NullString: sql.NullString{String: "nz", Valid: true}}, WebsiteID: 2, GroupID: 3, Name: "Kiwi", SortOrder: 30, IsActive: true},
		),
	)

	stg, err := nsg.Store(config.ScopeCode("nz"))
	assert.Nil(t, stg)
	assert.EqualError(t, store.ErrGroupNotFound, err.Error())

	stgs, err := nsg.Stores()
	assert.Nil(t, stgs)
	assert.EqualError(t, store.ErrGroupNotFound, err.Error())

}
Ejemplo n.º 12
0
func TestNewManagerWebsite(t *testing.T) {

	var managerWebsite = getTestManager(func(ms *mockStorage) {
		ms.w = func() (*store.Website, error) {
			return store.NewWebsite(
				&store.TableWebsite{WebsiteID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "euro", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Europe", Valid: true}}, SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: true, Valid: true}}},
			), nil
		}
	})

	tests := []struct {
		m               *store.Manager
		have            config.ScopeIDer
		wantErr         error
		wantWebsiteCode string
	}{
		{managerWebsite, nil, store.ErrAppStoreNotSet, ""},
		{getTestManager(), config.ScopeID(20), store.ErrGroupNotFound, ""},
		{managerWebsite, config.ScopeID(1), nil, "euro"},
		{managerWebsite, config.ScopeID(1), nil, "euro"},
		{managerWebsite, config.ScopeCode("notImportant"), nil, "euro"},
		{managerWebsite, config.ScopeCode("notImportant"), nil, "euro"},
	}

	for _, test := range tests {
		haveW, haveErr := test.m.Website(test.have)
		if test.wantErr != nil {
			assert.Error(t, haveErr, "%#v", test)
			assert.Nil(t, haveW, "%#v", test)
		} else {
			assert.NoError(t, haveErr, "%#v", test)
			assert.NotNil(t, haveW, "%#v", test)
			assert.Equal(t, test.wantWebsiteCode, haveW.Data.Code.String)
		}
	}
	assert.False(t, managerWebsite.IsCacheEmpty())
	managerWebsite.ClearCache()
	assert.True(t, managerWebsite.IsCacheEmpty())

}
Ejemplo n.º 13
0
// MBA mid 2012 CPU: Intel Core i5-3427U CPU @ 1.80GHz
// BenchmarkStorageStoreGetWebsite	 2000000	       656 ns/op	     176 B/op	       6 allocs/op
func BenchmarkStorageStoreGetWebsite(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var err error
		benchmarkStorageStore, err = testStorage.Store(config.ScopeCode("de"))
		if err != nil {
			b.Error(err)
		}

		benchmarkStorageStoreWebsite = benchmarkStorageStore.Website()
		if benchmarkStorageStoreWebsite == nil {
			b.Error("benchmarkStorageStoreWebsite is nil")
		}
	}
}
Ejemplo n.º 14
0
// MBA mid 2012 CPU: Intel Core i5-3427U CPU @ 1.80GHz
// BenchmarkStorageWebsiteGetDefaultGroup	  200000	      6081 ns/op	    1712 B/op	      45 allocs/op
func BenchmarkStorageWebsiteGetDefaultGroup(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var err error
		benchmarkStorageWebsite, err = testStorage.Website(config.ScopeCode("euro"))
		if err != nil {
			b.Error(err)
		}

		benchmarkStorageWebsiteDefaultGroup, err = benchmarkStorageWebsite.DefaultGroup()
		if err != nil {
			b.Error(err)
		}
	}
}
Ejemplo n.º 15
0
func TestClaim(t *testing.T) {
	s := store.NewStore(
		&store.TableStore{StoreID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "de", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true},
		&store.TableWebsite{WebsiteID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "admin", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Admin", Valid: true}}, SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}},
		&store.TableGroup{GroupID: 1, WebsiteID: 0, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0},
	)
	token := jwt.New(jwt.SigningMethodHS256)
	s.AddClaim(token)
	sCode := store.GetCodeFromClaim(token)
	assert.EqualValues(t, config.ScopeCode("de"), sCode)
	assert.Nil(t, store.GetCodeFromClaim(nil))

	token2 := jwt.New(jwt.SigningMethodHS256)
	token2.Claims[store.CookieName] = "Invalid Cod€"
	sCode2 := store.GetCodeFromClaim(token2)
	assert.Nil(t, sCode2)
}
Ejemplo n.º 16
0
func TestNewManagerGetRequestStore_ScopeStore(t *testing.T) {

	testCode := config.ScopeCode("de")
	testScope := config.ScopeStoreID

	if haveStore, haveErr := storeManagerRequestStore.GetRequestStore(config.ScopeID(1), testScope); haveErr == nil {
		t.Error("appStore should not be set!")
		t.Fail()
	} else {
		assert.Nil(t, haveStore)
		assert.EqualError(t, store.ErrAppStoreNotSet, haveErr.Error())
	}

	// init with scope store
	if err := storeManagerRequestStore.Init(testCode, testScope); err != nil {
		t.Error(err)
		t.Fail()
	}
	assert.EqualError(t, store.ErrAppStoreSet, storeManagerRequestStore.Init(testCode, testScope).Error())

	if s, err := storeManagerRequestStore.Store(); err == nil {
		assert.EqualValues(t, "de", s.Data.Code.String)
	} else {
		assert.EqualError(t, err, store.ErrStoreNotFound.Error())
		t.Fail()
	}

	tests := []testNewManagerGetRequestStore{
		{config.ScopeID(232), "", store.ErrStoreNotFound},
		{nil, "", store.ErrStoreNotFound},
		{config.ScopeCode("\U0001f631"), "", store.ErrStoreNotFound},

		{config.ScopeID(6), "nz", nil},
		{config.ScopeCode("ch"), "", store.ErrStoreNotActive},

		{config.ScopeCode("nz"), "nz", nil},
		{config.ScopeCode("de"), "de", nil},
		{config.ScopeID(2), "at", nil},

		{config.ScopeID(2), "at", nil},
		{config.ScopeCode("au"), "au", nil},
		{config.ScopeCode("ch"), "", store.ErrStoreNotActive},
	}
	runNewManagerGetRequestStore(t, testScope, tests)
}
Ejemplo n.º 17
0
// cyclomatic complexity 12 of function TestInitByRequest() is high (> 10) (gocyclo)
func TestInitByRequest(t *testing.T) {
	tests := []struct {
		req                  *http.Request
		haveR                config.ScopeIDer
		haveScopeType        config.ScopeGroup
		wantStoreCode        string // this is the default store in a scope, lookup in storeManagerRequestStore
		wantRequestStoreCode config.ScopeCoder
		wantErr              error
		wantCookie           string
	}{
		{
			getTestRequest(t, "GET", "http://cs.io", &http.Cookie{Name: store.CookieName, Value: "uk"}),
			config.ScopeID(1), config.ScopeStoreID, "de", config.ScopeCode("uk"), nil, "",
		},
		{
			getTestRequest(t, "GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=uk", nil),
			config.ScopeID(1), config.ScopeStoreID, "de", config.ScopeCode("uk"), nil, store.CookieName + "=uk;", // generates a new 1y valid cookie
		},
		{
			getTestRequest(t, "GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=%20uk", nil),
			config.ScopeID(1), config.ScopeStoreID, "de", config.ScopeCode("uk"), store.ErrStoreNotFound, "",
		},

		{
			getTestRequest(t, "GET", "http://cs.io", &http.Cookie{Name: store.CookieName, Value: "de"}),
			config.ScopeID(1), config.ScopeGroupID, "at", config.ScopeCode("de"), nil, "",
		},
		{
			getTestRequest(t, "GET", "http://cs.io", nil),
			config.ScopeID(1), config.ScopeGroupID, "at", nil, nil, "",
		},
		{
			getTestRequest(t, "GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=de", nil),
			config.ScopeID(1), config.ScopeGroupID, "at", config.ScopeCode("de"), nil, store.CookieName + "=de;", // generates a new 1y valid cookie
		},
		{
			getTestRequest(t, "GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=at", nil),
			config.ScopeID(1), config.ScopeGroupID, "at", config.ScopeCode("at"), nil, store.CookieName + "=;", // generates a delete cookie
		},
		{
			getTestRequest(t, "GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=cz", nil),
			config.ScopeID(1), config.ScopeGroupID, "at", nil, store.ErrStoreNotFound, "",
		},
		{
			getTestRequest(t, "GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=uk", nil),
			config.ScopeID(1), config.ScopeGroupID, "at", nil, store.ErrStoreChangeNotAllowed, "",
		},

		{
			getTestRequest(t, "GET", "http://cs.io", &http.Cookie{Name: store.CookieName, Value: "nz"}),
			config.ScopeID(2), config.ScopeWebsiteID, "au", config.ScopeCode("nz"), nil, "",
		},
		{
			getTestRequest(t, "GET", "http://cs.io", &http.Cookie{Name: store.CookieName, Value: "n'z"}),
			config.ScopeID(2), config.ScopeWebsiteID, "au", nil, nil, "",
		},
		{
			getTestRequest(t, "GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=uk", nil),
			config.ScopeID(2), config.ScopeWebsiteID, "au", nil, store.ErrStoreChangeNotAllowed, "",
		},
		{
			getTestRequest(t, "GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=nz", nil),
			config.ScopeID(2), config.ScopeWebsiteID, "au", config.ScopeCode("nz"), nil, store.CookieName + "=nz;",
		},
		{
			getTestRequest(t, "GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=ch", nil),
			config.ScopeID(1), config.ScopeWebsiteID, "at", nil, store.ErrStoreNotActive, "",
		},
		{
			getTestRequest(t, "GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=nz", nil),
			config.ScopeID(1), config.ScopeDefaultID, "at", config.ScopeCode("nz"), nil, "",
		},
	}

	for _, test := range tests {
		if _, haveErr := storeManagerRequestStore.InitByRequest(nil, nil, test.haveScopeType); haveErr != nil {
			assert.EqualError(t, store.ErrAppStoreNotSet, haveErr.Error())
		} else {
			t.Fatal("InitByRequest should return an error if used without running Init() first.")
		}

		if err := storeManagerRequestStore.Init(test.haveR, test.haveScopeType); err != nil {
			assert.EqualError(t, store.ErrUnsupportedScopeGroup, err.Error())
			t.Log("continuing for loop because of expected store.ErrUnsupportedScopeGroup")
			storeManagerRequestStore.ClearCache(true)
			continue
		}

		if s, err := storeManagerRequestStore.Store(); err == nil {
			assert.EqualValues(t, test.wantStoreCode, s.Data.Code.String)
		} else {
			assert.EqualError(t, err, store.ErrStoreNotFound.Error())
			t.Log("continuing for loop because of expected store.ErrStoreNotFound")
			storeManagerRequestStore.ClearCache(true)
			continue
		}

		resRec := httptest.NewRecorder()

		haveStore, haveErr := storeManagerRequestStore.InitByRequest(resRec, test.req, test.haveScopeType)
		if test.wantErr != nil {
			assert.Nil(t, haveStore)
			assert.EqualError(t, test.wantErr, haveErr.Error())
		} else {
			if msg, ok := haveErr.(errgo.Locationer); ok {
				t.Logf("\nLocation: %s => %s\n", haveErr, msg.Location())
			}
			assert.NoError(t, haveErr, "%#v", test)
			if test.wantRequestStoreCode != nil {
				assert.NotNil(t, haveStore, "%#v", test.req.URL.Query())
				assert.EqualValues(t, test.wantRequestStoreCode.ScopeCode(), haveStore.Data.Code.String)

				newKeks := resRec.HeaderMap.Get("Set-Cookie")
				if test.wantCookie != "" {
					assert.Contains(t, newKeks, test.wantCookie, "%#v", test)
					//					t.Logf(
					//						"\nwantRequestStoreCode: %s\nCookie Str: %#v\n",
					//						test.wantRequestStoreCode.Code(),
					//						newKeks,
					//					)
				} else {
					assert.Empty(t, newKeks, "%#v", test)
				}

			} else {
				assert.Nil(t, haveStore, "%#v", haveStore)
			}
		}
		storeManagerRequestStore.ClearCache(true)
	}
}
Ejemplo n.º 18
0
func TestInitByToken(t *testing.T) {

	getToken := func(code string) *jwt.Token {
		t := jwt.New(jwt.SigningMethodHS256)
		t.Claims[store.CookieName] = code
		return t
	}

	tests := []struct {
		haveR              config.ScopeIDer
		haveCodeToken      string
		haveScopeType      config.ScopeGroup
		wantStoreCode      string // this is the default store in a scope, lookup in storeManagerRequestStore
		wantTokenStoreCode config.ScopeCoder
		wantErr            error
	}{
		{config.ScopeCode("de"), "de", config.ScopeStoreID, "de", config.ScopeCode("de"), nil},
		{config.ScopeCode("de"), "at", config.ScopeStoreID, "de", config.ScopeCode("at"), nil},
		{config.ScopeCode("de"), "a$t", config.ScopeStoreID, "de", nil, nil},
		{config.ScopeCode("at"), "ch", config.ScopeStoreID, "at", nil, store.ErrStoreNotActive},
		{config.ScopeCode("at"), "", config.ScopeStoreID, "at", nil, nil},

		{config.ScopeID(1), "de", config.ScopeGroupID, "at", config.ScopeCode("de"), nil},
		{config.ScopeID(1), "ch", config.ScopeGroupID, "at", nil, store.ErrStoreNotActive},
		{config.ScopeID(1), " ch", config.ScopeGroupID, "at", nil, nil},
		{config.ScopeID(1), "uk", config.ScopeGroupID, "at", nil, store.ErrStoreChangeNotAllowed},

		{config.ScopeID(2), "uk", config.ScopeWebsiteID, "au", nil, store.ErrStoreChangeNotAllowed},
		{config.ScopeID(2), "nz", config.ScopeWebsiteID, "au", config.ScopeCode("nz"), nil},
		{config.ScopeID(2), "n z", config.ScopeWebsiteID, "au", nil, nil},
		{config.ScopeID(2), "", config.ScopeWebsiteID, "au", nil, nil},
	}
	for _, test := range tests {

		haveStore, haveErr := storeManagerRequestStore.InitByToken(nil, test.haveScopeType)
		assert.Nil(t, haveStore)
		assert.EqualError(t, store.ErrAppStoreNotSet, haveErr.Error())

		if err := storeManagerRequestStore.Init(test.haveR, test.haveScopeType); err != nil {
			t.Fatal(err)
		}

		if s, err := storeManagerRequestStore.Store(); err == nil {
			assert.EqualValues(t, test.wantStoreCode, s.Data.Code.String)
		} else {
			assert.EqualError(t, err, store.ErrStoreNotFound.Error())
			t.Fail()
		}

		haveStore, haveErr = storeManagerRequestStore.InitByToken(getToken(test.haveCodeToken), test.haveScopeType)
		if test.wantErr != nil {
			assert.Nil(t, haveStore, "%#v", test)
			assert.Error(t, haveErr, "%#v", test)
			assert.EqualError(t, test.wantErr, haveErr.Error())
		} else {
			if test.wantTokenStoreCode != nil {
				assert.NotNil(t, haveStore, "%#v", test)
				assert.NoError(t, haveErr)
				assert.Equal(t, test.wantTokenStoreCode.ScopeCode(), haveStore.Data.Code.String)
			} else {
				assert.Nil(t, haveStore, "%#v", test)
				assert.NoError(t, haveErr, "%#v", test)
			}

		}
		storeManagerRequestStore.ClearCache(true)
	}
}
Ejemplo n.º 19
0
func TestNewManagerError(t *testing.T) {
	err := getTestManager().Init(config.ScopeCode("euro"), config.ScopeDefaultID)
	assert.EqualError(t, err, store.ErrUnsupportedScopeGroup.Error())
}