Пример #1
0
func init() {
	middlewareConfigReader = config.NewMockGetter(
		config.WithMockValues(config.MockPV{
			scope.StrDefault.FQPathInt64(0, backend.Backend.WebURLRedirectToBase.String()):  1,
			scope.StrStores.FQPathInt64(1, backend.Backend.WebSecureUseInFrontend.String()): true,
			scope.StrStores.FQPathInt64(1, backend.Backend.WebUnsecureBaseURL.String()):     "http://www.corestore.io/",
			scope.StrStores.FQPathInt64(1, backend.Backend.WebSecureBaseURL.String()):       "https://www.corestore.io/",
		}),
	)

	middlewareCtxStoreService = storemock.WithContextMustService(
		scope.Option{},
		func(ms *storemock.Storage) {
			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},
					store.WithStoreConfig(middlewareConfigReader),
				)
			}
		},
	)
}
Пример #2
0
func TestStoreBaseURLandPath(t *testing.T) {
	defer debugLogBuf.Reset()

	s, err := 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("admin"), Name: dbr.NewNullString("Admin"), SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NewNullBool(false)},
		&store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "Default", RootCategoryID: 0, DefaultStoreID: 1},
	)
	assert.NoError(t, err)
	if s == nil {
		t.Fail()
	}

	tests := []struct {
		haveR        config.Getter
		haveUT       config.URLType
		haveIsSecure bool
		wantBaseUrl  string
		wantPath     string
	}{
		{
			config.NewMockGetter(config.WithMockString(
				func(path string) (string, error) {

					switch path {
					// scope is here store but config.ScopedGetter must fall back to default
					case backend.Backend.WebSecureBaseURL.FQPathInt64(scope.StrDefault, 0):
						return "https://corestore.io", nil
					case backend.Backend.WebUnsecureBaseURL.FQPathInt64(scope.StrDefault, 0):
						return "http://corestore.io", nil
					}
					return "", config.ErrKeyNotFound
				},
			)),
			config.URLTypeWeb, true, "https://corestore.io/", "/",
		},
		{
			config.NewMockGetter(config.WithMockString(
				func(path string) (string, error) {
					switch path {
					case backend.Backend.WebSecureBaseURL.FQPathInt64(scope.StrDefault, 0):
						return "https://myplatform.io/customer1", nil
					case backend.Backend.WebUnsecureBaseURL.FQPathInt64(scope.StrDefault, 0):
						return "http://myplatform.io/customer1", nil
					}
					return "", config.ErrKeyNotFound
				},
			)),
			config.URLTypeWeb, false, "http://myplatform.io/customer1/", "/customer1/",
		},
		{
			config.NewMockGetter(config.WithMockString(
				func(path string) (string, error) {
					switch path {
					case backend.Backend.WebSecureBaseURL.FQPathInt64(scope.StrDefault, 0):
						return model.PlaceholderBaseURL, nil
					case backend.Backend.WebUnsecureBaseURL.FQPathInt64(scope.StrDefault, 0):
						return model.PlaceholderBaseURL, nil
					case scope.StrDefault.FQPathInt64(0, config.PathCSBaseURL):
						return config.CSBaseURL, nil
					}
					return "", config.ErrKeyNotFound
				},
			)),
			config.URLTypeWeb, false, config.CSBaseURL, "/",
		},
	}

	for i, test := range tests {
		s.ApplyOptions(store.WithStoreConfig(test.haveR))
		assert.NotNil(t, s.Config, "Index %d", i)
		baseURL, err := s.BaseURL(test.haveUT, test.haveIsSecure)
		assert.NoError(t, err)
		assert.EqualValues(t, test.wantBaseUrl, baseURL.String())
		assert.EqualValues(t, test.wantPath, s.Path())

		_, err = s.BaseURL(config.URLTypeAbsent, false)
		assert.EqualError(t, err, config.ErrURLCacheCleared.Error())
	}
	t.Log(debugLogBuf.String()) // bug in config ScopedGetter
}