Beispiel #1
0
func TestWithValidateBaseUrl_ActivatedAndShouldNotRedirectWithPOSTRequest(t *testing.T) {

	mockReader := config.NewMockReader(
		config.WithMockValues(config.MockPV{
			config.MockPathScopeDefault(store.PathRedirectToBase): 301,
		}),
	)

	w := httptest.NewRecorder()
	req, err := http.NewRequest(httputils.MethodGet, "http://corestore.io/catalog/product/view", nil)
	assert.NoError(t, err)

	mw := store.WithValidateBaseURL(mockReader)(finalHandlerWithValidateBaseURL(t))

	err = mw.ServeHTTPContext(context.Background(), w, req)
	assert.EqualError(t, err, store.ErrContextServiceNotFound.Error())

	w = httptest.NewRecorder()
	req, err = http.NewRequest(httputils.MethodPost, "http://corestore.io/catalog/product/view", strings.NewReader(`{ "k1": "v1",  "k2": { "k3": ["va1"]  }}`))
	assert.NoError(t, err)

	err = mw.ServeHTTPContext(context.Background(), w, req)
	assert.NoError(t, err)

}
Beispiel #2
0
func TestAllowedCountriesDefault(t *testing.T) {
	cr := config.NewMockReader(
		config.WithMockValues(config.MockPV{}),
	)

	haveCountries, err := directory.AllowedCountries(cr.NewScoped(1, 1, 1))
	assert.NoError(t, err)
	assert.True(t, len(haveCountries) > 100)
}
Beispiel #3
0
func TestContextMustReader(t *testing.T) {
	mr := config.NewMockReader()
	ctx := config.NewContextReader(context.Background(), mr)
	mrHave := config.FromContextReader(ctx)
	assert.Exactly(t, mr, mrHave)

	ctx = config.NewContextReader(context.Background(), nil)
	mrHave = config.FromContextReader(ctx)
	assert.Exactly(t, config.DefaultManager, mrHave)
}
Beispiel #4
0
func TestAllowedCountriesFound(t *testing.T) {
	cr := config.NewMockReader(
		config.WithMockValues(config.MockPV{
			config.MockPathScopeStore(1, directory.PathCountryAllowed): "DE,AU,CH,AT",
		}),
	)

	haveCountries, err := directory.AllowedCountries(cr.NewScoped(1, 1, 1))
	assert.NoError(t, err)
	assert.Exactly(t, utils.StringSlice{"DE", "AU", "CH", "AT"}, haveCountries)
}
Beispiel #5
0
func TestContextMustReaderPubSuber(t *testing.T) {
	mr := config.NewMockReader()
	ctx := config.NewContextReaderPubSuber(context.Background(), mr)
	mrHave, ok := config.FromContextReaderPubSuber(ctx)
	assert.Exactly(t, mr, mrHave)
	assert.True(t, ok)

	ctx = config.NewContextReaderPubSuber(context.Background(), nil)
	mrHave, ok = config.FromContextReaderPubSuber(ctx)
	assert.Nil(t, mrHave)
	assert.False(t, ok)
}
Beispiel #6
0
func TestContextMustReaderPubSuber(t *testing.T) {
	mr := config.NewMockReader()
	ctx := context.WithValue(context.Background(), config.CtxKeyReaderPubSuber, mr)
	assert.Exactly(t, mr, config.ContextMustReaderPubSuber(ctx))

	defer func() {
		if r := recover(); r != nil {
			assert.EqualError(t, r.(error), config.ErrTypeAssertionReaderPubSuberFailed.Error())
		}
	}()
	ctx = context.WithValue(context.Background(), config.CtxKeyReaderPubSuber, "Hello")
	config.ContextMustReaderPubSuber(ctx)
}
Beispiel #7
0
func TestWithValidateBaseUrl_DeactivatedAndShouldNotRedirectWithGETRequest(t *testing.T) {

	mockReader := config.NewMockReader(
		config.WithMockValues(config.MockPV{
			config.MockPathScopeDefault(store.PathRedirectToBase): 0,
		}),
	)

	// no post request but check deactivated
	w := httptest.NewRecorder()
	req, err := http.NewRequest(httputils.MethodGet, "http://corestore.io/catalog/product/view", nil)
	assert.NoError(t, err)

	err = store.WithValidateBaseURL(mockReader)(finalHandlerWithValidateBaseURL(t)).ServeHTTPContext(context.Background(), w, req)
	assert.NoError(t, err)
}
Beispiel #8
0
func TestPasswordFromConfig(t *testing.T) {

	cfg := config.NewMockReader(
		config.WithMockValues(config.MockPV{
			config.MockPathScopeDefault(ctxjwt.PathJWTPassword): `Rump3lst!lzch3n`,
		}),
	)

	jm, err := ctxjwt.NewService(
		ctxjwt.WithPasswordFromConfig(cfg),
	)
	assert.NoError(t, err)

	theToken, _, err := jm.GenerateToken(nil)
	assert.NoError(t, err)
	assert.NotEmpty(t, theToken)

}
Beispiel #9
0
func TestPasswordFromConfig(t *testing.T) {

	cfg := config.NewMockReader(
		config.MockString(func(path string) string {
			if path == userjwt.PathJWTPassword {
				return `Rump3lst!lzch3n`
			}
			return ""
		}),
	)

	jm, err := userjwt.New(
		userjwt.SetPasswordFromConfig(cfg),
	)
	assert.NoError(t, err)

	token, _, err := jm.GenerateToken(nil)
	assert.NoError(t, err)
	assert.NotEmpty(t, token)

}
Beispiel #10
0
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())

}
Beispiel #11
0
func TestSourceCurrencyAll(t *testing.T) {

	r := config.NewMockReader(
		config.WithMockString(func(path string) (string, error) {
			t.Log(path)
			switch path {
			case config.MockPathScopeStore(1, directory.PathDefaultLocale):
				return "de_CH", nil
			}
			return "Not Found", nil
		}),
	)

	var s scope.MockID = 1

	sca := directory.NewSourceCurrencyAll(config.ModelConstructor{
		ConfigReader: r,
		ScopeStore:   s,
	})

	t.Logf("\n%#v\n", sca.Options())

}
Beispiel #12
0
func init() {
	middlewareConfigReader = config.NewMockReader(
		config.WithMockValues(config.MockPV{
			config.MockPathScopeDefault(store.PathRedirectToBase):    1,
			config.MockPathScopeStore(1, store.PathSecureInFrontend): true,
			config.MockPathScopeStore(1, store.PathUnsecureBaseURL):  "http://www.corestore.io/",
			config.MockPathScopeStore(1, store.PathSecureBaseURL):    "https://www.corestore.io/",
		}),
	)

	middlewareCtxStoreService = storemock.NewContextService(
		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.SetStoreConfig(middlewareConfigReader),
				)
			}
		},
	)
}
Beispiel #13
0
func TestHTTPRateLimitConfig(t *testing.T) {
	cr := config.NewMockReader(
		config.WithMockValues(config.MockPV{
			config.MockPathScopeDefault(ctxthrottled.PathRateLimitBurst):    0,
			config.MockPathScopeDefault(ctxthrottled.PathRateLimitRequests): 1,
			config.MockPathScopeDefault(ctxthrottled.PathRateLimitDuration): "i",
		}),
	)

	limiter := ctxthrottled.HTTPRateLimit{
		Config: cr,
		VaryBy: &pathGetter{},
	}

	handler := limiter.WithRateLimit(nil, ctxhttp.HandlerFunc(func(_ context.Context, w http.ResponseWriter, _ *http.Request) error {
		w.WriteHeader(200)
		return nil
	}))

	runHTTPTestCases(t, handler, []httpTestCase{
		{"xx", 200, map[string]string{"X-Ratelimit-Limit": "1", "X-Ratelimit-Remaining": "0", "X-Ratelimit-Reset": "60"}},
		{"xx", 429, map[string]string{"X-Ratelimit-Limit": "1", "X-Ratelimit-Remaining": "0", "X-Ratelimit-Reset": "60", "Retry-After": "60"}},
	})
}
Beispiel #14
0
func TestStoreBaseURLandPath(t *testing.T) {

	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.Reader
		haveUT       config.URLType
		haveIsSecure bool
		wantBaseUrl  string
		wantPath     string
	}{
		{
			config.NewMockReader(config.WithMockString(
				func(path string) (string, error) {
					switch path {
					case config.MockPathScopeDefault(store.PathSecureBaseURL):
						return "https://corestore.io", nil
					case config.MockPathScopeDefault(store.PathUnsecureBaseURL):
						return "http://corestore.io", nil
					}
					return "", config.ErrKeyNotFound
				},
			)),
			config.URLTypeWeb, true, "https://corestore.io/", "/",
		},
		{
			config.NewMockReader(config.WithMockString(
				func(path string) (string, error) {
					switch path {
					case config.MockPathScopeDefault(store.PathSecureBaseURL):
						return "https://myplatform.io/customer1", nil
					case config.MockPathScopeDefault(store.PathUnsecureBaseURL):
						return "http://myplatform.io/customer1", nil
					}
					return "", config.ErrKeyNotFound
				},
			)),
			config.URLTypeWeb, false, "http://myplatform.io/customer1/", "/customer1/",
		},
		{
			config.NewMockReader(config.WithMockString(
				func(path string) (string, error) {
					switch path {
					case config.MockPathScopeDefault(store.PathSecureBaseURL):
						return store.PlaceholderBaseURL, nil
					case config.MockPathScopeDefault(store.PathUnsecureBaseURL):
						return store.PlaceholderBaseURL, nil
					case config.MockPathScopeDefault(config.PathCSBaseURL):
						return config.CSBaseURL, nil
					}
					return "", config.ErrKeyNotFound
				},
			)),
			config.URLTypeWeb, false, config.CSBaseURL, "/",
		},
	}

	for i, test := range tests {
		s.ApplyOptions(store.SetStoreConfig(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())
	}
}
Beispiel #15
0
var configMock = config.NewMockReader(
	config.MockInt(func(path string) int {
		//		println("int", path)
		switch path {
		case "stores/5015/system/smtp/port":
			return 0
		case "stores/6023/system/smtp/port":
			return 4040
		default:
			return 0
		}
	}),
	config.MockString(func(path string) string {
		//		println("string", path)
		switch path {
		case "stores/5015/system/smtp/host":
			return ""
		case "stores/5015/system/smtp/username":
			return ""
		case "stores/6023/system/smtp/host":
			return "smtp.fastmail.com"
		case "stores/6023/system/smtp/username":
			return "2522e71a49e"
		case "stores/6023/system/smtp/password":
			return "9512e71a49f"
		default:
			return ""
		}

	}),
	config.MockBool(func(path string) bool {
		return false
	}),
)
Beispiel #16
0
	"github.com/corestoreio/csfw/utils/mail"
	"github.com/go-gomail/gomail"
	"github.com/stretchr/testify/assert"
)

var configMock = config.NewMockReader(
	config.MockInt(func(path string) int {
		//println("int", path)
		return 25 // Port 25
	}),
	config.MockString(func(path string) string {
		//println("string", path)
		return "localhost"
	}),
	config.MockBool(func(path string) bool {
		//println("bool", path)
		switch path {
		case "stores/3001/system/smtp/disable":
			return true
		case "stores/4010/system/smtp/disable":
			return false
		default:
			return false
		}
	}),
)

type mockDial struct {
	t        *testing.T
	dialErr  error
	sendErr  error
Beispiel #17
0
func TestStoreBaseUrlandPath(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},
	)

	tests := []struct {
		haveR        config.Reader
		haveUT       config.URLType
		haveIsSecure bool
		wantBaseUrl  string
		wantPath     string
	}{
		{
			config.NewMockReader(func(path string) string {
				switch path {
				case config.ScopeRangeDefault + "/0/" + store.PathSecureBaseURL:
					return "https://corestore.io"
				case config.ScopeRangeDefault + "/0/" + store.PathUnsecureBaseURL:
					return "http://corestore.io"
				}
				return ""
			}, nil),
			config.URLTypeWeb, true, "https://corestore.io/", "/",
		},
		{
			config.NewMockReader(func(path string) string {
				switch path {
				case config.ScopeRangeDefault + "/0/" + store.PathSecureBaseURL:
					return "https://myplatform.io/customer1"
				case config.ScopeRangeDefault + "/0/" + store.PathUnsecureBaseURL:
					return "http://myplatform.io/customer1"
				}
				return ""
			}, nil),
			config.URLTypeWeb, false, "http://myplatform.io/customer1/", "/customer1/",
		},
		{
			config.NewMockReader(func(path string) string {
				switch path {
				case config.ScopeRangeDefault + "/0/" + store.PathSecureBaseURL:
					return store.PlaceholderBaseURL
				case config.ScopeRangeDefault + "/0/" + store.PathUnsecureBaseURL:
					return store.PlaceholderBaseURL
				case config.ScopeRangeDefault + "/0/" + config.PathCSBaseURL:
					return config.CSBaseURL
				}
				return ""
			}, nil),
			config.URLTypeWeb, false, config.CSBaseURL, "/",
		},
	}

	for _, test := range tests {
		s.ApplyOptions(store.SetStoreConfig(test.haveR))
		assert.EqualValues(t, test.wantBaseUrl, s.BaseURL(test.haveUT, test.haveIsSecure))
		assert.EqualValues(t, test.wantPath, s.Path())
	}
}