func TestNewManagerGroupInit(t *testing.T) { err := getTestManager(func(ms *mockStorage) { ms.g = func() (*store.Group, error) { return store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, store.SetGroupWebsite(&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.ScopeID(1), config.ScopeGroupID) assert.EqualError(t, store.ErrGroupDefaultStoreNotFound, err.Error(), "Incorrect DefaultStore for a Group") err = getTestManager().Init(config.ScopeID(21), config.ScopeGroupID) assert.EqualError(t, store.ErrGroupNotFound, err.Error()) tm3 := getTestManager(func(ms *mockStorage) { ms.g = func() (*store.Group, error) { return store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, store.SetGroupWebsite(&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}}}), ).SetStores(store.TableStoreSlice{ &store.TableStore{StoreID: 2, Code: dbr.NullString{NullString: sql.NullString{String: "at", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Österreich", SortOrder: 20, IsActive: true}, }, nil), nil } }) err = tm3.Init(config.ScopeID(1), config.ScopeGroupID) assert.NoError(t, err) g, err := tm3.Group() assert.NoError(t, err) assert.NotNil(t, g) assert.Equal(t, int64(2), g.Data.DefaultStoreID) }
func TestNewManagerStoreInit(t *testing.T) { tms := getTestManager(func(ms *mockStorage) { ms.s = func() (*store.Store, error) { return 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: "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}}}, &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, ), nil } }) tests := []struct { haveManager *store.Manager haveID config.ScopeIDer wantErr error }{ {tms, config.ScopeID(1), nil}, {tms, config.ScopeID(1), store.ErrAppStoreSet}, {tms, nil, store.ErrAppStoreSet}, {tms, nil, store.ErrAppStoreSet}, } for _, test := range tests { haveErr := test.haveManager.Init(test.haveID, config.ScopeStoreID) if test.wantErr != nil { assert.Error(t, haveErr) assert.EqualError(t, test.wantErr, haveErr.Error()) } else { assert.NoError(t, haveErr) } s, err := test.haveManager.Store() assert.NotNil(t, s) assert.NoError(t, err) } }
func TestDialerPoolSingleton(t *testing.T) { dm1, err := NewDaemon( SetConfig(configMock), SetScope(config.ScopeID(6023)), ) assert.NoError(t, err) assert.NotNil(t, dm1) assert.Equal(t, uint64(0x96b8eb270abcef94), dm1.ID()) // "smtp.fastmail.com40402522e71a49e" dm2, err := NewDaemon( SetConfig(configMock), SetScope(config.ScopeID(6023)), ) assert.NoError(t, err) assert.NotNil(t, dm2) // t.Logf("%p == %p", dm1.dialer, dm2.dialer) dp1 := dm1.dialer dp2 := dm2.dialer assert.True(t, dp1 == dp2, "dm1.dialer != dm2.dialer but must be equal") dm3, err := NewDaemon( SetConfig(configMock), SetScope(config.ScopeID(7077)), ) assert.NoError(t, err) assert.NotNil(t, dm3) dp3 := dm3.dialer assert.True(t, dp1 == dp2 && dp1 != dp3 && dp2 != dp3, "dm1.dialer == dm2.dialer && dm1.dialer != dm3.dialer") }
// DefaultStoreView traverses through the websites to find the default website and gets // the default group which has the default store id assigned to. Only one website can be the default one. func (st *Storage) DefaultStoreView() (*Store, error) { for _, website := range st.websites { if website.IsDefault.Bool && website.IsDefault.Valid { g, err := st.group(config.ScopeID(website.DefaultGroupID)) if err != nil { return nil, err } return st.Store(config.ScopeID(g.DefaultStoreID)) } } return nil, ErrStoreNotFound }
// NewDaemon creates a new mail sending daemon to send to a SMTP server. // Per default it uses localhost:25, creates an unbuffered channel, uses the // config.DefaultManager, applies the admin scope (0) and sets the SMTP // timeout to 30s. func NewDaemon(opts ...DaemonOption) (*Daemon, error) { d := &Daemon{ config: config.DefaultManager, scopeID: config.ScopeID(0), // Default Scope aka Admin Scope smtpTimeout: time.Second * 30, } d.SetOptions(opts...) if d.IsOffline() { SetSendFunc(OfflineSend)(d) } if d.msgChan == nil { d.msgChan = make(chan *gomail.Message) } if nil == d.dialer && nil == d.sendFunc { d.dialer = dialerPool.allocatePlain(d) } if d.lastErrs != nil { return nil, d // because Daemon implements error interface } return d, nil }
// NewDaemon creates a new mail sending daemon to send to a SMTP server. // Per default it uses localhost:25, creates an unbuffered channel, uses the // config.DefaultManager, applies the admin scope (0) and sets the SMTP // timeout to 30s. func NewDaemon(opts ...DaemonOption) (*Daemon, error) { d := &Daemon{ Config: config.DefaultManager, ScopeID: config.ScopeID(0), // Default Scope aka Admin Scope SmtpTimeout: time.Second * 30, } d.SetOptions(opts...) if d.IsOffline() { SetSendFunc(OfflineSend)(d) } if d.msgChan == nil { d.msgChan = make(chan *gomail.Message) } if nil == d.sendFunc && nil == d.dialer { d.lastErrs = append(d.lastErrs, errors.New("Missing a Dialer or SendFunc. Please set them via DaemonOption")) } if d.lastErrs != nil { return nil, d // because Daemon implements error interface } return d, nil }
func TestDaemonOfflineLogger(t *testing.T) { offLog := mail.OfflineLogger defer func() { mail.OfflineLogger = offLog }() var logBufI bytes.Buffer var logBufE bytes.Buffer mail.OfflineLogger = log.NewStdLogger( log.SetStdLevel(log.StdLevelInfo), log.SetStdInfo(&logBufI, "test", std.LstdFlags), log.SetStdError(&logBufE, "test", std.LstdFlags), ) dm, err := mail.NewDaemon() dm.Config = configMock dm.ScopeID = config.ScopeID(3001) assert.NoError(t, err) assert.NotNil(t, dm) assert.True(t, dm.IsOffline()) go func() { assert.NoError(t, dm.Worker()) }() assert.NoError(t, dm.SendPlain("gopher@earth", "apple@mothership", "Phoning home", "Hey Apple stop phoning home or you become apple puree")) assert.NoError(t, dm.Stop()) assert.True(t, mail.OfflineLogger.IsInfo()) time.Sleep(time.Millisecond) // waiting for channel to drain assert.Contains(t, logBufI.String(), `Send from: "gopher@earth" to: []string{"apple@mothership"} msg: "Mime-Version: 1.0`) assert.Empty(t, logBufE.String()) }
func TestDaemonOffline(t *testing.T) { offSend := mail.OfflineSend defer func() { mail.OfflineSend = offSend }() mail.OfflineSend = func(from string, to []string, msg io.WriterTo) error { var buf bytes.Buffer _, err := msg.WriteTo(&buf) assert.NoError(t, err) assert.Equal(t, "gopher@world", from) assert.Equal(t, []string{"apple@cupertino"}, to) assert.Contains(t, buf.String(), "phoning home") assert.Contains(t, buf.String(), "Subject: Phoning home") return nil } dm, err := mail.NewDaemon() dm.Config = configMock dm.ScopeID = config.ScopeID(3001) assert.NoError(t, err) assert.NotNil(t, dm) assert.True(t, dm.IsOffline()) go func() { assert.NoError(t, dm.Worker()) }() assert.NoError(t, dm.SendPlain("gopher@world", "apple@cupertino", "Phoning home", "Hey Apple stop phoning home or you become apple puree")) assert.NoError(t, dm.Stop()) assert.EqualError(t, dm.Worker(), mail.ErrMailChannelClosed.Error()) assert.EqualError(t, dm.Stop(), mail.ErrMailChannelClosed.Error()) assert.EqualError(t, dm.Send(nil), mail.ErrMailChannelClosed.Error()) assert.EqualError(t, dm.SendPlain("", "", "", ""), mail.ErrMailChannelClosed.Error()) assert.EqualError(t, dm.SendHtml("", "", "", ""), mail.ErrMailChannelClosed.Error()) }
func TestDaemonWorkerReDialCloseError(t *testing.T) { defer errLogBuf.Reset() dm, err := mail.NewDaemon( mail.SetConfig(configMock), mail.SetScope(config.ScopeID(4010)), mail.SetDialer( mockDial{ t: t, closeErr: errors.New("Test Close Error"), }, ), ) assert.NoError(t, err) assert.NotNil(t, dm) assert.False(t, dm.IsOffline()) go func() { assert.EqualError(t, dm.Worker(), "Test Close Error", "See goroutine") }() assert.NoError(t, dm.SendPlain("rust@lang", "apple@cupertino", "Spagetti", "Pastafari meets Rustafari")) time.Sleep(time.Millisecond * 100) assert.NoError(t, dm.Stop()) assert.Contains(t, errLogBuf.String(), "mail.daemon.workerDial.timeout.Close err: Test Close Error") }
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()) }
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()) }
func TestNewManagerGroup(t *testing.T) { var managerGroupSimpleTest = getTestManager(func(ms *mockStorage) { ms.g = func() (*store.Group, error) { return store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, store.SetGroupWebsite(&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 } ms.s = func() (*store.Store, error) { return 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: "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}}}, &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, ), nil } }) tests := []struct { m *store.Manager have config.ScopeIDer wantErr error wantGroupName string wantWebsiteCode string }{ {managerGroupSimpleTest, nil, store.ErrAppStoreNotSet, "", ""}, {getTestManager(), config.ScopeID(20), store.ErrGroupNotFound, "", ""}, {managerGroupSimpleTest, config.ScopeID(1), nil, "DACH Group", "euro"}, {managerGroupSimpleTest, config.ScopeID(1), nil, "DACH Group", "euro"}, } for _, test := range tests { g, err := test.m.Group(test.have) if test.wantErr != nil { assert.Nil(t, g) assert.EqualError(t, test.wantErr, err.Error(), "test %#v", test) } else { assert.NotNil(t, g, "test %#v", test) assert.NoError(t, err, "test %#v", test) assert.Equal(t, test.wantGroupName, g.Data.Name) assert.Equal(t, test.wantWebsiteCode, g.Website.Data.Code.String) } } assert.False(t, managerGroupSimpleTest.IsCacheEmpty()) managerGroupSimpleTest.ClearCache() assert.True(t, managerGroupSimpleTest.IsCacheEmpty()) }
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) }
func TestDialerPoolDefaultConfig(t *testing.T) { dm, err := NewDaemon( SetConfig(configMock), SetScope(config.ScopeID(5015)), ) assert.NoError(t, err) assert.NotNil(t, dm) assert.Equal(t, uint64(0xcc72e0b18f4a60fb), dm.ID()) // "localhost25" }
// Store creates a new Store which contains the the store, its group and website // according to the interface definition. func (st *Storage) Store(r config.ScopeIDer) (*Store, error) { s, err := st.store(r) if err != nil { return nil, errgo.Mask(err) } w, err := st.website(config.ScopeID(s.WebsiteID)) if err != nil { return nil, errgo.Mask(err) } g, err := st.group(config.ScopeID(s.GroupID)) if err != nil { return nil, errgo.Mask(err) } ns := NewStore(s, w, g, SetStoreConfig(st.cr)) ns.Website().SetGroupsStores(st.groups, st.stores) ns.Group().SetStores(st.stores, w) return ns, nil }
// Stores creates a new store slice. Can return an error when the website or // the group cannot be found. func (st *Storage) Stores() (StoreSlice, error) { stores := make(StoreSlice, len(st.stores), len(st.stores)) for i, s := range st.stores { var err error if stores[i], err = st.Store(config.ScopeID(s.StoreID)); err != nil { return nil, errgo.Mask(err) } } return stores, nil }
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()) }
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) }
// Groups creates a new group slice containing its website all related stores. // May panic when a website pointer is nil. func (st *Storage) Groups() (GroupSlice, error) { groups := make(GroupSlice, len(st.groups), len(st.groups)) for i, g := range st.groups { w, err := st.website(config.ScopeID(g.WebsiteID)) if err != nil { return nil, errgo.Mask(err) } groups[i] = NewGroup(g, SetGroupConfig(st.cr), SetGroupWebsite(w)).SetStores(st.stores, nil) } return groups, nil }
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) }
// Group creates a new Group which contains all related stores and its website according to the // interface definition. func (st *Storage) Group(id config.ScopeIDer) (*Group, error) { g, err := st.group(id) if err != nil { return nil, err } w, err := st.website(config.ScopeID(g.WebsiteID)) if err != nil { return nil, err } return NewGroup(g, SetGroupWebsite(w), SetGroupConfig(st.cr)).SetStores(st.stores, nil), nil }
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()) }
// MBA mid 2012 CPU: Intel Core i5-3427U CPU @ 1.80GHz // BenchmarkStorageGroupGetDefaultStore 1000000 1916 ns/op 464 B/op 14 allocs/op func BenchmarkStorageGroupGetDefaultStore(b *testing.B) { for i := 0; i < b.N; i++ { var err error benchmarkStorageGroup, err = testStorage.Group(config.ScopeID(3)) if err != nil { b.Error(err) } benchmarkStorageGroupDefaultStore, err = benchmarkStorageGroup.DefaultStore() if err != nil { b.Error(err) } } }
func TestStorageGroup(t *testing.T) { tests := []struct { id config.ScopeIDer err error wantName string }{ {nil, store.ErrGroupNotFound, ""}, {config.ScopeID(2015), store.ErrGroupNotFound, ""}, {config.ScopeID(1), nil, "DACH Group"}, } for _, test := range tests { g, err := testStorage.Group(test.id) if test.err != nil { assert.Nil(t, g) assert.EqualError(t, test.err, err.Error()) } else { assert.NotNil(t, g) assert.NoError(t, err) assert.Equal(t, test.wantName, g.Data().Name) } } g, err := testStorage.Group(config.ScopeID(3)) assert.NoError(t, err) assert.NotNil(t, g) dStore, err := g.DefaultStore() assert.NoError(t, err) assert.EqualValues(t, "au", dStore.Data().Code.String) assert.EqualValues(t, "oz", g.Website().Data().Code.String) stores, err := g.Stores() assert.NoError(t, err) assert.EqualValues(t, utils.StringSlice{"au", "nz"}, stores.Codes()) }
func TestDaemonWorkerDialSend(t *testing.T) { dm, err := mail.NewDaemon( mail.SetConfig(configMock), mail.SetScope(config.ScopeID(4010)), mail.SetDialer( mockDial{t: t}, ), ) assert.NoError(t, err) assert.NotNil(t, dm) assert.False(t, dm.IsOffline()) go func() { assert.NoError(t, dm.Worker()) }() assert.NoError(t, dm.SendPlain("rust@lang", "apple@cupertino", "Spagetti", "Pastafari meets Rustafari")) assert.NoError(t, dm.Stop()) }
func TestStorageGroupNoWebsite(t *testing.T) { var tst = store.NewStorage( store.SetStorageWebsites( &store.TableWebsite{WebsiteID: 21, 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: 3, WebsiteID: 2, Name: "Australia", RootCategoryID: 2, DefaultStoreID: 5}, ), store.SetStorageStores( &store.TableStore{StoreID: 5, 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}, ), ) g, err := tst.Group(config.ScopeID(3)) assert.Nil(t, g) assert.EqualError(t, store.ErrWebsiteNotFound, err.Error()) gs, err := tst.Groups() assert.Nil(t, gs) assert.EqualError(t, store.ErrWebsiteNotFound, err.Error()) }
func TestSourceCurrencyAll(t *testing.T) { r := config.NewMockReader( config.MockString(func(path string) string { t.Log(path) switch path { case config.MockPathScopeStore(1, directory.PathDefaultLocale): return "de_CH" } return "Not Found" }), ) s := config.ScopeID(1) sca := directory.NewSourceCurrencyAll(config.ModelConstructor{ ConfigReader: r, Scope: s, }) t.Logf("\n%#v\n", sca.Options()) }
// 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) } }
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) } }