Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fixes #5 and #3

goenning91810bf96bb6
diff --git a/.env b/.env
index 84e27212b..882ffab4e 100644
--- a/.env
+++ b/.env
@@ -1 +1,2 @@
-DATABASE_URL=postgres://wchy:wchy-pw@localhost:5555/wchy?sslmode=disable
\ No newline at end of file
+DATABASE_URL=postgres://wchy:wchy-pw@localhost:5555/wchy?sslmode=disable
+GO_ENV=development
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 7c45e4048..c2ae7f4c0 100644
--- a/Makefile
+++ b/Makefile
@@ -13,7 +13,7 @@ define tag_docker
endef
test:
- go test $$(go list ./... | grep -v /vendor/) -cover
+ GO_ENV=test go test $$(go list ./... | grep -v /vendor/) -cover
lint:
golint -set_exit_status
@@ -33,7 +33,7 @@ run-ci: lint
docker push wecanhearyou/wchy-api
migrate:
-ifeq ($(ENV), development)
+ifeq ($(GO_ENV), development)
migrate -url postgres://wchy:wchy-pw@localhost:5555/wchy?sslmode=disable -path ./migrations up
else
migrate -url ${DATABASE_URL} -path ./migrations up
diff --git a/main.go b/main.go
index 97a846d5a..23504f5dd 100644
--- a/main.go
+++ b/main.go
@@ -20,7 +20,13 @@ var db *sql.DB
var buildtime string
func init() {
+ goEnv := os.Getenv("GO_ENV")
log.Info("Application is starting...")
+ log.Infof("GO_ENV: %s", goEnv)
+ if goEnv == "production" || goEnv == "staging" {
+ os.Setenv("GIN_MODE", "release")
+ }
+
db, _ = sql.Open("postgres", os.Getenv("DATABASE_URL"))
ctx = context.WchyContext{
diff --git a/services/tenant.go b/services/tenant.go
index 494ba641c..6c11fa379 100644
--- a/services/tenant.go
+++ b/services/tenant.go
@@ -2,8 +2,10 @@ package services
import (
"database/sql"
+ "strings"
"github.com/WeCanHearYou/wchy-api/models"
+ "github.com/WeCanHearYou/wchy-api/util"
)
// TenantService contains read and write operations for tenants
@@ -35,11 +37,19 @@ type PostgresTenantService struct {
func (svc PostgresTenantService) GetByDomain(domain string) (*models.Tenant, error) {
tenant := &models.Tenant{}
- row := svc.DB.QueryRow("SELECT id, name, subdomain FROM tenants WHERE subdomain = $1", domain)
+ row := svc.DB.QueryRow("SELECT id, name, subdomain FROM tenants WHERE subdomain = $1", extractSubdomain(domain))
err := row.Scan(&tenant.ID, &tenant.Name, &tenant.Domain)
if err == sql.ErrNoRows {
return nil, ErrNotFound
}
+ tenant.Domain = tenant.Domain + "." + util.GetCurrentDomain()
return tenant, nil
}
+
+func extractSubdomain(domain string) string {
+ if idx := strings.Index(domain, "."); idx != -1 {
+ return domain[:idx]
+ }
+ return domain
+}
diff --git a/services/tenant_test.go b/services/tenant_test.go
index ff70870ac..240daf48b 100644
--- a/services/tenant_test.go
+++ b/services/tenant_test.go
@@ -10,7 +10,7 @@ import (
sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
)
-func TestTenantByDomain_WhenDomainIsUnknown_ShouldReturnError(t *testing.T) {
+func TestTenantByDomain_WhenSubdomainIsUnknown_ShouldReturnError(t *testing.T) {
db, mock, _ := sqlmock.New()
defer db.Close()
@@ -23,7 +23,7 @@ func TestTenantByDomain_WhenDomainIsUnknown_ShouldReturnError(t *testing.T) {
assert.Error(t, err)
}
-func TestTenantByDomain_WhenDomainIsKnown_ShouldReturnTenantInformation(t *testing.T) {
+func TestTenantByDomain_WhenSubdomainIsKnown_ShouldReturnTenantInformation(t *testing.T) {
db, mock, _ := sqlmock.New()
defer db.Close()
@@ -33,8 +33,24 @@ func TestTenantByDomain_WhenDomainIsKnown_ShouldReturnTenantInformation(t *testi
svc := &services.PostgresTenantService{DB: db}
tenant, err := svc.GetByDomain("mydomain")
- assert.Equal(t, tenant.ID, 234)
- assert.Equal(t, tenant.Name, "My Domain Inc.")
- assert.Equal(t, tenant.Domain, "mydomain")
+ assert.Equal(t, 234, tenant.ID)
+ assert.Equal(t, "My Domain Inc.", tenant.Name)
+ assert.Equal(t, "mydomain.test.canhearyou.com", tenant.Domain)
+ assert.Nil(t, err)
+}
+
+func TestTenantByDomain_WhenCompleteDomainIsUsed_ShouldReturnTenantInformation(t *testing.T) {
+ db, mock, _ := sqlmock.New()
+ defer db.Close()
+
+ rows := sqlmock.NewRows([]string{"id", "name", "subdomain"}).AddRow(234, "My Domain Inc.", "mydomain")
+ mock.ExpectQuery("SELECT id, name, subdomain FROM tenants WHERE subdomain = \\$1").WithArgs("mydomain").WillReturnRows(rows)
+
+ svc := &services.PostgresTenantService{DB: db}
+ tenant, err := svc.GetByDomain("mydomain.anydomain.com")
+
+ assert.Equal(t, 234, tenant.ID)
+ assert.Equal(t, "My Domain Inc.", tenant.Name)
+ assert.Equal(t, "mydomain.test.canhearyou.com", tenant.Domain)
assert.Nil(t, err)
}
diff --git a/util/env.go b/util/env.go
index 446917c9d..549c8128c 100644
--- a/util/env.go
+++ b/util/env.go
@@ -11,3 +11,18 @@ func GetEnvOrDefault(name string, defaultValue string) string {
}
return value
}
+
+// GetCurrentDomain returns WCHY domain based on current environment variables
+func GetCurrentDomain() string {
+ env := os.Getenv("GO_ENV")
+ switch env {
+ case "test":
+ return "test.canhearyou.com"
+ case "development":
+ return "dev.canhearyou.com"
+ case "staging":
+ return "staging.canhearyou.com"
+ }
+ return "canhearyou.com"
+
+}
diff --git a/util/env_test.go b/util/env_test.go
index 10157ab7f..d6436bdb7 100644
--- a/util/env_test.go
+++ b/util/env_test.go
@@ -3,6 +3,8 @@ package util_test
import (
"testing"
+ "os"
+
"github.com/WeCanHearYou/wchy-api/util"
"github.com/stretchr/testify/assert"
)
@@ -16,3 +18,22 @@ func TestGetEnvOrDefault_ShouldReturnEnvironmentValue(t *testing.T) {
value := util.GetEnvOrDefault("PATH", "default path")
assert.NotEqual(t, "default path", value)
}
+
+func TestGetCurrentDomain_ShouldReturnCorrectDomain(t *testing.T) {
+ var tests = []struct {
+ env string
+ expected string
+ }{
+ {"test", "test.canhearyou.com"},
+ {"staging", "staging.canhearyou.com"},
+ {"development", "dev.canhearyou.com"},
+ {"production", "canhearyou.com"},
+ {"anything", "canhearyou.com"},
+ }
+
+ for _, testCase := range tests {
+ os.Setenv("GO_ENV", testCase.env)
+ actual := util.GetCurrentDomain()
+ assert.Equal(t, testCase.expected, actual)
+ }
+}