fix: disable mutex for testing purpose

This commit is contained in:
Lakhan Samani 2022-05-31 15:00:11 +05:30
parent 98015708a2
commit 69b56c9912
7 changed files with 44 additions and 32 deletions

View File

@ -10,7 +10,7 @@ build-dashboard:
clean: clean:
rm -rf build rm -rf build
test: test:
rm -rf server/test/test.db && rm -rf test.db && cd server && go clean --testcache && go test -v ./test rm -rf server/test/test.db && rm -rf test.db && cd server && go clean --testcache && go test -p 1 -v ./test
generate: generate:
cd server && go get github.com/99designs/gqlgen/cmd@v0.14.0 && go run github.com/99designs/gqlgen generate cd server && go get github.com/99designs/gqlgen/cmd@v0.14.0 && go run github.com/99designs/gqlgen generate

View File

@ -12,8 +12,8 @@ type EnvStore struct {
// UpdateEnvStore to update the whole env store object // UpdateEnvStore to update the whole env store object
func (e *EnvStore) UpdateStore(store map[string]interface{}) { func (e *EnvStore) UpdateStore(store map[string]interface{}) {
e.mutex.Lock() // e.mutex.Lock()
defer e.mutex.Unlock() // defer e.mutex.Unlock()
// just override the keys + new keys // just override the keys + new keys
for key, value := range store { for key, value := range store {
@ -23,22 +23,22 @@ func (e *EnvStore) UpdateStore(store map[string]interface{}) {
// GetStore returns the env store // GetStore returns the env store
func (e *EnvStore) GetStore() map[string]interface{} { func (e *EnvStore) GetStore() map[string]interface{} {
e.mutex.Lock() // e.mutex.Lock()
defer e.mutex.Unlock() // defer e.mutex.Unlock()
return e.store return e.store
} }
// Get returns the value of the key in evn store // Get returns the value of the key in evn store
func (s *EnvStore) Get(key string) interface{} { func (e *EnvStore) Get(key string) interface{} {
s.mutex.Lock() // e.mutex.Lock()
defer s.mutex.Unlock() // defer e.mutex.Unlock()
return s.store[key] return e.store[key]
} }
// Set sets the value of the key in env store // Set sets the value of the key in env store
func (s *EnvStore) Set(key string, value interface{}) { func (e *EnvStore) Set(key string, value interface{}) {
s.mutex.Lock() // e.mutex.Lock()
defer s.mutex.Unlock() // defer e.mutex.Unlock()
s.store[key] = value e.store[key] = value
} }

View File

@ -7,8 +7,8 @@ import (
// ClearStore clears the in-memory store. // ClearStore clears the in-memory store.
func (c *provider) ClearStore() error { func (c *provider) ClearStore() error {
c.mutex.Lock() // c.mutex.Lock()
defer c.mutex.Unlock() // defer c.mutex.Unlock()
c.sessionStore = map[string]map[string]string{} c.sessionStore = map[string]map[string]string{}
return nil return nil
@ -16,8 +16,8 @@ func (c *provider) ClearStore() error {
// GetUserSessions returns all the user session token from the in-memory store. // GetUserSessions returns all the user session token from the in-memory store.
func (c *provider) GetUserSessions(userId string) map[string]string { func (c *provider) GetUserSessions(userId string) map[string]string {
c.mutex.Lock() // c.mutex.Lock()
defer c.mutex.Unlock() // defer c.mutex.Unlock()
res := map[string]string{} res := map[string]string{}
for k, v := range c.stateStore { for k, v := range c.stateStore {
split := strings.Split(v, "@") split := strings.Split(v, "@")
@ -31,8 +31,8 @@ func (c *provider) GetUserSessions(userId string) map[string]string {
// DeleteAllUserSession deletes all the user sessions from in-memory store. // DeleteAllUserSession deletes all the user sessions from in-memory store.
func (c *provider) DeleteAllUserSession(userId string) error { func (c *provider) DeleteAllUserSession(userId string) error {
c.mutex.Lock() // c.mutex.Lock()
defer c.mutex.Unlock() // defer c.mutex.Unlock()
sessions := c.GetUserSessions(userId) sessions := c.GetUserSessions(userId)
for k := range sessions { for k := range sessions {
c.RemoveState(k) c.RemoveState(k)
@ -43,8 +43,8 @@ func (c *provider) DeleteAllUserSession(userId string) error {
// SetState sets the state in the in-memory store. // SetState sets the state in the in-memory store.
func (c *provider) SetState(key, state string) error { func (c *provider) SetState(key, state string) error {
c.mutex.Lock() // c.mutex.Lock()
defer c.mutex.Unlock() // defer c.mutex.Unlock()
c.stateStore[key] = state c.stateStore[key] = state
return nil return nil
@ -52,8 +52,8 @@ func (c *provider) SetState(key, state string) error {
// GetState gets the state from the in-memory store. // GetState gets the state from the in-memory store.
func (c *provider) GetState(key string) (string, error) { func (c *provider) GetState(key string) (string, error) {
c.mutex.Lock() // c.mutex.Lock()
defer c.mutex.Unlock() // defer c.mutex.Unlock()
state := "" state := ""
if stateVal, ok := c.stateStore[key]; ok { if stateVal, ok := c.stateStore[key]; ok {
@ -65,8 +65,8 @@ func (c *provider) GetState(key string) (string, error) {
// RemoveState removes the state from the in-memory store. // RemoveState removes the state from the in-memory store.
func (c *provider) RemoveState(key string) error { func (c *provider) RemoveState(key string) error {
c.mutex.Lock() // c.mutex.Lock()
defer c.mutex.Unlock() // defer c.mutex.Unlock()
delete(c.stateStore, key) delete(c.stateStore, key)
return nil return nil

View File

@ -19,9 +19,10 @@ func TestResolvers(t *testing.T) {
for dbType, dbURL := range databases { for dbType, dbURL := range databases {
s := testSetup() s := testSetup()
defer s.Server.Close()
memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDatabaseURL, dbURL) memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDatabaseURL, dbURL)
memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDatabaseType, dbType) memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDatabaseType, dbType)
defer s.Server.Close()
err := db.InitDB() err := db.InitDB()
if err != nil { if err != nil {
t.Errorf("Error initializing database: %s", err) t.Errorf("Error initializing database: %s", err)

View File

@ -36,6 +36,7 @@ func revokeAccessTest(t *testing.T, s TestSetup) {
adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)
assert.Nil(t, err) assert.Nil(t, err)
h, err := crypto.EncryptPassword(adminSecret) h, err := crypto.EncryptPassword(adminSecret)
assert.Nil(t, err) assert.Nil(t, err)
req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h))

View File

@ -87,16 +87,26 @@ func testSetup() TestSetup {
log.Fatal("Error loading required env: ", err) log.Fatal("Error loading required env: ", err)
} }
memorystore.InitMemStore() err = memorystore.InitMemStore()
if err != nil {
log.Fatal("Error loading memory store: ", err)
}
memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpHost, "smtp.yopmail.com") memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpHost, "smtp.yopmail.com")
memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpPort, "2525") memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpPort, "2525")
memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpUsername, "lakhan@yopmail.com") memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpUsername, "lakhan@yopmail.com")
memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpPassword, "test") memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpPassword, "test")
memorystore.Provider.UpdateEnvVariable(constants.EnvKeySenderEmail, "info@yopmail.com") memorystore.Provider.UpdateEnvVariable(constants.EnvKeySenderEmail, "info@yopmail.com")
memorystore.Provider.UpdateEnvVariable(constants.EnvKeyProtectedRoles, "admin") memorystore.Provider.UpdateEnvVariable(constants.EnvKeyProtectedRoles, "admin")
memorystore.InitMemStore()
db.InitDB() err = db.InitDB()
env.InitAllEnv() if err != nil {
log.Fatal("Error loading db: ", err)
}
err = env.InitAllEnv()
if err != nil {
log.Fatal("Error loading env: ", err)
}
w := httptest.NewRecorder() w := httptest.NewRecorder()
c, r := gin.CreateTestContext(w) c, r := gin.CreateTestContext(w)

View File

@ -22,7 +22,7 @@ func TestIsValidEmail(t *testing.T) {
func TestIsValidOrigin(t *testing.T) { func TestIsValidOrigin(t *testing.T) {
// don't use portocal(http/https) for ALLOWED_ORIGINS while testing, // don't use portocal(http/https) for ALLOWED_ORIGINS while testing,
// as we trim them off while running the main function // as we trim them off while running the main function
memorystore.Provider.UpdateEnvVariable(constants.EnvKeyAllowedOrigins, []string{"localhost:8080", "*.google.com", "*.google.in", "*abc.*"}) memorystore.Provider.UpdateEnvVariable(constants.EnvKeyAllowedOrigins, "localhost:8080,*.google.com,*.google.in,*abc.*")
assert.False(t, validators.IsValidOrigin("http://myapp.com"), "it should be invalid origin") assert.False(t, validators.IsValidOrigin("http://myapp.com"), "it should be invalid origin")
assert.False(t, validators.IsValidOrigin("http://appgoogle.com"), "it should be invalid origin") assert.False(t, validators.IsValidOrigin("http://appgoogle.com"), "it should be invalid origin")
assert.True(t, validators.IsValidOrigin("http://app.google.com"), "it should be valid origin") assert.True(t, validators.IsValidOrigin("http://app.google.com"), "it should be valid origin")
@ -32,7 +32,7 @@ func TestIsValidOrigin(t *testing.T) {
assert.True(t, validators.IsValidOrigin("http://xyx.abc.in"), "it should be valid origin") assert.True(t, validators.IsValidOrigin("http://xyx.abc.in"), "it should be valid origin")
assert.True(t, validators.IsValidOrigin("http://xyxabc.in"), "it should be valid origin") assert.True(t, validators.IsValidOrigin("http://xyxabc.in"), "it should be valid origin")
assert.True(t, validators.IsValidOrigin("http://localhost:8080"), "it should be valid origin") assert.True(t, validators.IsValidOrigin("http://localhost:8080"), "it should be valid origin")
memorystore.Provider.UpdateEnvVariable(constants.EnvKeyAllowedOrigins, []string{"*"}) memorystore.Provider.UpdateEnvVariable(constants.EnvKeyAllowedOrigins, "*")
} }
func TestIsValidIdentifier(t *testing.T) { func TestIsValidIdentifier(t *testing.T) {