func TestNewManagerStore(t *testing.T) { assert.True(t, managerStoreSimpleTest.IsCacheEmpty()) for j := 0; j < 3; j++ { s, err := managerStoreSimpleTest.Store(scope.MockCode("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 scope.StoreIDer wantErr error }{ {scope.MockCode("nilSlices"), store.ErrStoreNotFound}, {scope.MockID(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()) }
func TestMockCode(t *testing.T) { assert.Equal(t, "Waverly", scope.MockCode("Waverly").StoreCode()) assert.Equal(t, "Waverly", scope.MockCode("Waverly").WebsiteCode()) var i int64 = -1 assert.Equal(t, i, scope.MockCode("Waverly").WebsiteID()) assert.Equal(t, i, scope.MockCode("Waverly").GroupID()) assert.Equal(t, i, scope.MockCode("Waverly").StoreID()) }
func TestMustNewService(t *testing.T) { defer func() { if r := recover(); r != nil { assert.EqualError(t, r.(error), store.ErrStoreNotFound.Error()) } else { t.Fatal("Expecting a Panic") } }() tests := []struct { have scope.StoreIDer wantErr error }{ {scope.MockCode("nilSlices"), store.ErrStoreNotFound}, {scope.MockID(2), store.ErrStoreNotFound}, {nil, store.ErrStoreNotFound}, } serviceEmpty := storemock.MustNewService(scope.Option{}) for _, test := range tests { s, err := serviceEmpty.Store(test.have) assert.Nil(t, s) assert.EqualError(t, test.wantErr, err.Error()) } assert.True(t, serviceStoreSimpleTest.IsCacheEmpty()) }
func TestWithInitStoreByToken(t *testing.T) { var newReq = func(i int) *http.Request { req, err := http.NewRequest(httputils.MethodGet, fmt.Sprintf("https://corestore.io/store/list/%d", i), nil) if err != nil { t.Fatal(err) } return req } tests := []struct { ctx context.Context wantStoreCode string wantErr error }{ {store.NewContextReader(context.Background(), nil), "de", store.ErrContextServiceNotFound}, {store.NewContextReader(context.Background(), getInitializedStoreService(scope.Option{Store: scope.MockCode("de")})), "de", ctxjwt.ErrContextJWTNotFound}, {newStoreServiceWithTokenCtx(scope.Option{Store: scope.MockCode("de")}, "de"), "de", nil}, {newStoreServiceWithTokenCtx(scope.Option{Store: scope.MockCode("at")}, "ch"), "at", store.ErrStoreNotActive}, {newStoreServiceWithTokenCtx(scope.Option{Store: scope.MockCode("de")}, "at"), "at", nil}, {newStoreServiceWithTokenCtx(scope.Option{Store: scope.MockCode("de")}, "a$t"), "de", store.ErrStoreCodeInvalid}, {newStoreServiceWithTokenCtx(scope.Option{Store: scope.MockCode("at")}, ""), "at", store.ErrStoreCodeInvalid}, {newStoreServiceWithTokenCtx(scope.Option{Group: scope.MockID(1)}, "de"), "de", nil}, {newStoreServiceWithTokenCtx(scope.Option{Group: scope.MockID(1)}, "ch"), "at", store.ErrStoreNotActive}, {newStoreServiceWithTokenCtx(scope.Option{Group: scope.MockID(1)}, " ch"), "at", store.ErrStoreCodeInvalid}, {newStoreServiceWithTokenCtx(scope.Option{Group: scope.MockID(1)}, "uk"), "at", store.ErrStoreChangeNotAllowed}, {newStoreServiceWithTokenCtx(scope.Option{Website: scope.MockID(2)}, "uk"), "au", store.ErrStoreChangeNotAllowed}, {newStoreServiceWithTokenCtx(scope.Option{Website: scope.MockID(2)}, "nz"), "nz", nil}, {newStoreServiceWithTokenCtx(scope.Option{Website: scope.MockID(2)}, "n z"), "au", store.ErrStoreCodeInvalid}, {newStoreServiceWithTokenCtx(scope.Option{Website: scope.MockID(2)}, "au"), "au", nil}, {newStoreServiceWithTokenCtx(scope.Option{Website: scope.MockID(2)}, ""), "au", store.ErrStoreCodeInvalid}, } for i, test := range tests { mw := store.WithInitStoreByToken()(finalInitStoreHandler(t, test.wantStoreCode)) rec := httptest.NewRecorder() surfErr := mw.ServeHTTPContext(test.ctx, rec, newReq(i)) if test.wantErr != nil { assert.EqualError(t, surfErr, test.wantErr.Error(), "Index %d", i) continue } assert.NoError(t, surfErr, "Index %d", i) } }
func TestNewServiceWebsite(t *testing.T) { var serviceWebsite = storemock.MustNewService(scope.Option{}, func(ms *storemock.Storage) { ms.MockWebsite = func() (*store.Website, error) { return store.NewWebsite( &store.TableWebsite{WebsiteID: 1, Code: dbr.NewNullString("euro"), Name: dbr.NewNullString("Europe"), SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NewNullBool(true)}, ) } ms.MockStore = func() (*store.Store, error) { return store.NewStore( &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("euro"), Name: dbr.NewNullString("Europe"), SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NewNullBool(true)}, &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, ) } }) tests := []struct { m *store.Service have scope.WebsiteIDer wantErr error wantWebsiteCode string }{ {serviceWebsite, scope.MockID(1), nil, "euro"}, {serviceWebsite, scope.MockID(1), nil, "euro"}, {serviceWebsite, scope.MockCode("notImportant"), nil, "euro"}, {serviceWebsite, scope.MockCode("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, serviceWebsite.IsCacheEmpty()) serviceWebsite.ClearCache() assert.True(t, serviceWebsite.IsCacheEmpty()) }
func TestNewManagerGetRequestStore_ScopeGroup(t *testing.T) { testOption := scope.Option{Group: scope.MockID(1)} testScope := scope.GroupID if haveStore, haveErr := storeManagerRequestStore.GetRequestStore(testOption, 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.ErrIDNotFoundTableGroupSlice, storeManagerRequestStore.Init(scope.Option{Group: scope.MockID(123)}).Error()) if err := storeManagerRequestStore.Init(testOption); err != nil { t.Error(err) t.Fail() } assert.EqualError(t, store.ErrAppStoreSet, storeManagerRequestStore.Init(testOption).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 g, err := storeManagerRequestStore.Group(); err == nil { assert.EqualValues(t, 1, g.Data.GroupID) } else { assert.EqualError(t, err, store.ErrStoreNotFound.Error()) t.Fail() } // we're testing here against Group ID = 1 tests := []testNewManagerGetRequestStore{ {scope.Option{Group: scope.MockID(232)}, "", store.ErrIDNotFoundTableGroupSlice}, {scope.Option{Store: scope.MockID(232)}, "", store.ErrIDNotFoundTableStoreSlice}, {scope.Option{}, "", store.ErrUnsupportedScope}, {scope.Option{Store: scope.MockCode("\U0001f631")}, "", store.ErrIDNotFoundTableStoreSlice}, {scope.Option{Store: scope.MockID(6)}, "nz", store.ErrStoreChangeNotAllowed}, {scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive}, {scope.Option{Store: scope.MockCode("de")}, "de", nil}, {scope.Option{Store: scope.MockID(2)}, "at", nil}, {scope.Option{Store: scope.MockID(2)}, "at", nil}, {scope.Option{Store: scope.MockCode("au")}, "au", store.ErrStoreChangeNotAllowed}, {scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive}, {scope.Option{Group: scope.MockCode("ch")}, "", store.ErrIDNotFoundTableGroupSlice}, {scope.Option{Group: scope.MockID(2)}, "", store.ErrStoreChangeNotAllowed}, {scope.Option{Group: scope.MockID(1)}, "at", nil}, {scope.Option{Website: scope.MockCode("xxxx")}, "", store.ErrIDNotFoundTableWebsiteSlice}, {scope.Option{Website: scope.MockID(2)}, "", store.ErrStoreChangeNotAllowed}, {scope.Option{Website: scope.MockID(1)}, "at", nil}, } runNewManagerGetRequestStore(t, scope.GroupID, tests) }
func TestNewManagerReInit(t *testing.T) { t.Skip(TODO_Better_Test_Data) // quick implement, use mock of dbr.SessionRunner and remove connection dbc := csdb.MustConnectTest() defer dbc.Close() dbrSess := dbc.NewSession() storeManager := store.NewManager(store.NewStorage(nil /* trick it*/)) if err := storeManager.ReInit(dbrSess); err != nil { t.Fatal(err) } tests := []struct { have scope.StoreIDer wantErr error }{ {scope.MockCode("dede"), nil}, {scope.MockCode("czcz"), store.ErrIDNotFoundTableStoreSlice}, {scope.MockID(1), nil}, {scope.MockID(100), store.ErrStoreNotFound}, {mockIDCode{1, "dede"}, nil}, {mockIDCode{2, "czfr"}, 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, "No Err; for test: %#v", test) assert.NotNil(t, s) // assert.NotEmpty(t, s.Data.Code.String, "%#v", s.Data) } else { assert.Error(t, err, "Err for test: %#v", test) assert.EqualError(t, test.wantErr, err.Error(), "EqualErr for test: %#v", test) assert.Nil(t, s) } } assert.False(t, storeManager.IsCacheEmpty()) storeManager.ClearCache() assert.True(t, storeManager.IsCacheEmpty()) }
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(scope.Option{Website: scope.MockCode("euro")}) 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(scope.Option{Website: scope.MockCode("euro")}) 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(scope.Option{Website: scope.MockCode("euronen")}) assert.Error(t, err3, "scope.MockCode(euro), config.ScopeWebsite: %#v => %s", err3, err3) assert.EqualError(t, store.ErrWebsiteNotFound, err3.Error()) }
func TestStorageStore(t *testing.T) { tests := []struct { have scope.StoreIDer err error wantCode string }{ {nil, store.ErrStoreNotFound, ""}, {scope.MockID(2015), store.ErrIDNotFoundTableStoreSlice, ""}, {scope.MockID(1), nil, "de"}, {scope.MockCode("asia"), store.ErrIDNotFoundTableStoreSlice, ""}, {scope.MockCode("nz"), nil, "nz"}, {mockIDCode{4, "nz"}, nil, "nz"}, {mockIDCode{4, "auuuuu"}, store.ErrIDNotFoundTableStoreSlice, ""}, } for i, test := range tests { s, err := testStorage.Store(test.have) if test.err != nil { assert.Nil(t, s, "%#v", test) assert.EqualError(t, err, test.err.Error(), "Index: %d", i) } 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(scope.MockCode("at")) assert.NoError(t, err) assert.NotNil(t, s) assert.EqualValues(t, "DACH Group", s.Group.Data.Name) assert.EqualValues(t, "euro", s.Website.Data.Code.String) wg, err := s.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) }
func TestStorageWebsite(t *testing.T) { tests := []struct { have scope.WebsiteIDer err error wantWCode string }{ {nil, store.ErrWebsiteNotFound, ""}, {scope.MockID(2015), store.ErrIDNotFoundTableWebsiteSlice, ""}, {scope.MockID(1), nil, "euro"}, {scope.MockCode("asia"), store.ErrIDNotFoundTableWebsiteSlice, ""}, {scope.MockCode("oz"), nil, "oz"}, {mockIDCode{1, "oz"}, nil, "oz"}, {mockIDCode{1, "ozzz"}, store.ErrIDNotFoundTableWebsiteSlice, ""}, } 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(scope.MockCode("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) assert.NotNil(t, w.Groups) assert.EqualValues(t, utils.Int64Slice{1, 2}, w.Groups.IDs()) assert.NotNil(t, w.Stores) assert.EqualValues(t, utils.StringSlice{"de", "uk", "at", "ch"}, w.Stores.Codes()) }
// 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(scope.MockCode("de")) if err != nil { b.Error(err) } if benchmarkManagerStore == nil { b.Error("benchmarkManagerStore is nil") } } }
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(scope.MockCode("nz")) assert.Nil(t, stw) assert.EqualError(t, store.ErrIDNotFoundTableWebsiteSlice, err.Error()) stws, err := nsw.Stores() assert.Nil(t, stws) assert.EqualError(t, store.ErrIDNotFoundTableWebsiteSlice, 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(scope.MockCode("nz")) assert.Nil(t, stg) assert.EqualError(t, store.ErrIDNotFoundTableGroupSlice, err.Error()) stgs, err := nsg.Stores() assert.Nil(t, stgs) assert.EqualError(t, store.ErrIDNotFoundTableGroupSlice, err.Error()) }
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 scope.WebsiteIDer wantErr error wantWebsiteCode string }{ {managerWebsite, nil, store.ErrAppStoreNotSet, ""}, {getTestManager(), scope.MockID(20), store.ErrGroupNotFound, ""}, {managerWebsite, scope.MockID(1), nil, "euro"}, {managerWebsite, scope.MockID(1), nil, "euro"}, {managerWebsite, scope.MockCode("notImportant"), nil, "euro"}, {managerWebsite, scope.MockCode("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()) }
// BenchmarkServiceGetStore-4 5000000 256 ns/op 16 B/op 1 allocs/op func BenchmarkServiceGetStore(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { var err error benchmarkServiceStore, err = serviceStoreSimpleTest.Store(scope.MockCode("de")) if err != nil { b.Error(err) } if benchmarkServiceStore == nil { b.Error("benchmarkServiceStore is nil") } } }
func TestNewServiceStore(t *testing.T) { assert.False(t, serviceStoreSimpleTest.IsCacheEmpty()) for j := 0; j < 3; j++ { s, err := serviceStoreSimpleTest.Store(scope.MockCode("notNil")) assert.NoError(t, err) assert.NotNil(t, s) assert.EqualValues(t, "de", s.Data.Code.String) } assert.False(t, serviceStoreSimpleTest.IsCacheEmpty()) serviceStoreSimpleTest.ClearCache() assert.True(t, serviceStoreSimpleTest.IsCacheEmpty()) }
// 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(scope.MockCode("euro")) if err != nil { b.Error(err) } benchmarkStorageWebsiteDefaultGroup, err = benchmarkStorageWebsite.DefaultGroup() if err != nil { b.Error(err) } } }
// 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(scope.MockCode("de")) if err != nil { b.Error(err) } benchmarkStorageStoreWebsite = benchmarkStorageStore.Website if benchmarkStorageStoreWebsite == nil { b.Error("benchmarkStorageStoreWebsite is nil") } } }
func TestNewServiceRequestedStore_ScopeGroup(t *testing.T) { initScope := scope.Option{Group: scope.MockID(1)} sm := getInitializedStoreService(initScope) if haveStore, haveErr := sm.RequestedStore(initScope); haveErr != nil { t.Fatal(haveErr) } else { assert.NoError(t, haveErr) assert.Exactly(t, int64(2), haveStore.StoreID()) } if s, err := sm.Store(); err == nil { assert.EqualValues(t, "at", s.Data.Code.String) } else { assert.EqualError(t, err, store.ErrStoreNotFound.Error()) t.Fail() } if g, err := sm.Group(); err == nil { assert.EqualValues(t, 1, g.Data.GroupID) } else { assert.EqualError(t, err, store.ErrStoreNotFound.Error()) t.Fail() } // // we're testing here against Group ID = 1 tests := []testNewServiceRequestedStore{ {scope.Option{Group: scope.MockID(232)}, "", store.ErrIDNotFoundTableGroupSlice}, {scope.Option{Store: scope.MockID(232)}, "", store.ErrIDNotFoundTableStoreSlice}, {scope.Option{Store: scope.MockCode("\U0001f631")}, "", store.ErrIDNotFoundTableStoreSlice}, {scope.Option{Store: scope.MockID(6)}, "nz", store.ErrStoreChangeNotAllowed}, {scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive}, {scope.Option{Store: scope.MockCode("de")}, "de", nil}, {scope.Option{Store: scope.MockID(2)}, "at", nil}, {scope.Option{Store: scope.MockID(2)}, "at", nil}, {scope.Option{Store: scope.MockCode("au")}, "au", store.ErrStoreChangeNotAllowed}, {scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive}, {scope.Option{Group: scope.MockCode("ch")}, "", store.ErrIDNotFoundTableGroupSlice}, {scope.Option{Group: scope.MockID(2)}, "", store.ErrStoreChangeNotAllowed}, {scope.Option{Group: scope.MockID(1)}, "at", nil}, {scope.Option{Website: scope.MockCode("xxxx")}, "", store.ErrIDNotFoundTableWebsiteSlice}, {scope.Option{Website: scope.MockID(2)}, "", store.ErrStoreChangeNotAllowed}, {scope.Option{Website: scope.MockID(1)}, "at", nil}, } runTestsRequestedStore(t, sm, tests) }
func TestNewManagerGetRequestStore_ScopeStore(t *testing.T) { testCode := scope.MockCode("de") testScope := scope.StoreID if haveStore, haveErr := storeManagerRequestStore.GetRequestStore(scope.Option{Store: scope.MockID(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(scope.Option{Store: testCode}); err != nil { t.Error(err) t.Fail() } assert.EqualError(t, store.ErrAppStoreSet, storeManagerRequestStore.Init(scope.Option{Store: testCode}).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{ {scope.Option{Store: scope.MockID(232)}, "", store.ErrIDNotFoundTableStoreSlice}, {scope.Option{}, "", store.ErrUnsupportedScope}, {scope.Option{Store: scope.MockCode("\U0001f631")}, "", store.ErrIDNotFoundTableStoreSlice}, {scope.Option{Store: scope.MockID(6)}, "nz", nil}, {scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive}, {scope.Option{Store: scope.MockCode("nz")}, "nz", nil}, {scope.Option{Store: scope.MockCode("de")}, "de", nil}, {scope.Option{Store: scope.MockID(2)}, "at", nil}, {scope.Option{Store: scope.MockID(2)}, "at", nil}, {scope.Option{Store: scope.MockCode("au")}, "au", nil}, {scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive}, } runNewManagerGetRequestStore(t, testScope, tests) }
func TestNewServiceRequestedStore_ScopeStore(t *testing.T) { initScope := scope.Option{Store: scope.MockID(1)} sm := getInitializedStoreService(initScope) if haveStore, haveErr := sm.RequestedStore(initScope); haveErr != nil { t.Fatal(haveErr) } else { assert.NoError(t, haveErr) assert.Exactly(t, int64(1), haveStore.StoreID()) } if s, err := sm.Store(); err == nil { assert.EqualValues(t, "de", s.Data.Code.String) } else { assert.EqualError(t, err, store.ErrStoreNotFound.Error()) t.Fail() } if s, err := sm.Store(scope.MockID(123)); err == nil { assert.Error(t, err) t.Fail() } else { assert.Nil(t, s) assert.EqualError(t, err, store.ErrIDNotFoundTableStoreSlice.Error()) } tests := []testNewServiceRequestedStore{ {scope.Option{Store: scope.MockID(232)}, "", store.ErrIDNotFoundTableStoreSlice}, {scope.Option{}, "de", scope.ErrUnsupportedScope}, {scope.Option{Store: scope.MockCode("\U0001f631")}, "", store.ErrIDNotFoundTableStoreSlice}, {scope.Option{Store: scope.MockID(6)}, "nz", nil}, {scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive}, {scope.Option{Store: scope.MockCode("nz")}, "nz", nil}, {scope.Option{Store: scope.MockCode("de")}, "de", nil}, {scope.Option{Store: scope.MockID(2)}, "at", nil}, {scope.Option{Store: scope.MockID(2)}, "at", nil}, {scope.Option{Store: scope.MockCode("au")}, "au", nil}, {scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive}, } runTestsRequestedStore(t, sm, tests) }
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 { haveSO scope.Option haveCodeToken string haveScopeType scope.Scope wantStoreCode string // this is the default store in a scope, lookup in storeManagerRequestStore wantTokenStoreCode scope.StoreCoder // can be nil wantErr error }{ {scope.Option{Store: scope.MockCode("de")}, "de", scope.StoreID, "de", scope.MockCode("de"), nil}, {scope.Option{Store: scope.MockCode("de")}, "at", scope.StoreID, "de", scope.MockCode("at"), nil}, {scope.Option{Store: scope.MockCode("de")}, "a$t", scope.StoreID, "de", nil, nil}, {scope.Option{Store: scope.MockCode("at")}, "ch", scope.StoreID, "at", nil, store.ErrStoreNotActive}, {scope.Option{Store: scope.MockCode("at")}, "", scope.StoreID, "at", nil, nil}, {scope.Option{Group: scope.MockID(1)}, "de", scope.GroupID, "at", scope.MockCode("de"), nil}, {scope.Option{Group: scope.MockID(1)}, "ch", scope.GroupID, "at", nil, store.ErrStoreNotActive}, {scope.Option{Group: scope.MockID(1)}, " ch", scope.GroupID, "at", nil, nil}, {scope.Option{Group: scope.MockID(1)}, "uk", scope.GroupID, "at", nil, store.ErrStoreChangeNotAllowed}, {scope.Option{Website: scope.MockID(2)}, "uk", scope.WebsiteID, "au", nil, store.ErrStoreChangeNotAllowed}, {scope.Option{Website: scope.MockID(2)}, "nz", scope.WebsiteID, "au", scope.MockCode("nz"), nil}, {scope.Option{Website: scope.MockID(2)}, "n z", scope.WebsiteID, "au", nil, nil}, {scope.Option{Website: scope.MockID(2)}, "", scope.WebsiteID, "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.haveSO); 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).Claims, 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.StoreCode(), haveStore.Data.Code.String) } else { assert.Nil(t, haveStore, "%#v", test) assert.NoError(t, haveErr, "%#v", test) } } storeManagerRequestStore.ClearCache(true) } }
} return req } var testsInitByRequest = []struct { req *http.Request haveSO scope.Option haveScopeType scope.Scope wantStoreCode string // this is the default store in a scope, lookup in storeManagerRequestStore wantRequestStoreCode scope.StoreCoder // can be nil in tests wantErr error wantCookie string }{ { getTestRequest("GET", "http://cs.io", &http.Cookie{Name: store.CookieName, Value: "uk"}), scope.Option{Store: scope.MockID(1)}, scope.StoreID, "de", scope.MockCode("uk"), nil, store.CookieName + "=uk;", }, { getTestRequest("GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=uk", nil), scope.Option{Store: scope.MockID(1)}, scope.StoreID, "de", scope.MockCode("uk"), nil, store.CookieName + "=uk;", // generates a new 1y valid cookie }, { getTestRequest("GET", "http://cs.io/?"+store.HTTPRequestParamStore+"=%20uk", nil), scope.Option{Store: scope.MockID(1)}, scope.StoreID, "de", scope.MockCode("uk"), store.ErrStoreCodeInvalid, "", }, { getTestRequest("GET", "http://cs.io", &http.Cookie{Name: store.CookieName, Value: "de"}), scope.Option{Group: scope.MockID(1)}, scope.GroupID, "at", scope.MockCode("de"), nil, store.CookieName + "=de;", }, { getTestRequest("GET", "http://cs.io", nil),