Skip to content

Commit

Permalink
Merge pull request #1614 from stakwork/feat/migrate_org_to_workspace
Browse files Browse the repository at this point in the history
Changed organization to workspace
  • Loading branch information
elraphty authored Apr 24, 2024
2 parents 073adc6 + ac293a5 commit 7667276
Show file tree
Hide file tree
Showing 21 changed files with 2,569 additions and 1,014 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/frontend-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ jobs:
chmod 777 -R ./lnd;
chmod 777 -R ./proxy;
chmod 777 -R ./cln;
- name: Check for NODES
uses: nick-fields/retry@v2
with:
with:
timeout_minutes: 10
max_attempts: 3
command: |
Expand All @@ -53,12 +53,12 @@ jobs:
docker logs dave.sphinx
docker wait stack_relaysetup_1
cat stack/relay/NODES.json;
- name: Copy Node.json
uses: canastro/copy-file-action@master
with:
source: 'stack/relay/NODES.json'
target: 'tribes-frontend/cypress/fixtures/nodes.json'
source: "stack/relay/NODES.json"
target: "tribes-frontend/cypress/fixtures/nodes.json"

- name: Install Frontend Dependencies
working-directory: ./tribes-frontend
Expand All @@ -73,14 +73,14 @@ jobs:
run: |
sleep 20
yarn run cypress:run
- name: Upload Cypress logs
if: failure()
uses: actions/upload-artifact@v2
with:
name: cypress-logs
path: tribes-frontend/cypress/videos

- name: Stop Stack
working-directory: ./stack
run: docker-compose down
run: docker-compose down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 98 additions & 13 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

type database struct {
db *gorm.DB
getWorkspaceByUuid func(uuid string) Organization
getUserRoles func(uuid string, pubkey string) []UserRoles
getWorkspaceByUuid func(uuid string) Workspace
getUserRoles func(uuid string, pubkey string) []WorkspaceUserRoles
}

func NewDatabaseConfig(db *gorm.DB) *database {
Expand Down Expand Up @@ -65,17 +65,12 @@ func InitDB() {
db.AutoMigrate(&Channel{})
db.AutoMigrate(&LeaderBoard{})
db.AutoMigrate(&ConnectionCodes{})
db.AutoMigrate(&Bounty{})
db.AutoMigrate(&Organization{})
db.AutoMigrate(&OrganizationUsers{})
db.AutoMigrate(&BountyRoles{})
db.AutoMigrate(&UserRoles{})
db.AutoMigrate(&BountyBudget{})
db.AutoMigrate(&BudgetHistory{})
db.AutoMigrate(&PaymentHistory{})
db.AutoMigrate(&InvoiceList{})
db.AutoMigrate(&UserInvoiceData{})

DB.MigrateTablesWithOrgUuid()
DB.MigrateOrganizationToWorkspace()

people := DB.GetAllPeople()
for _, p := range people {
if p.Uuid == "" {
Expand Down Expand Up @@ -178,6 +173,96 @@ func (db database) GetRolesCount() int64 {
return count
}

func (db database) MigrateTablesWithOrgUuid() {
if !db.db.Migrator().HasTable("bounty") {
if !db.db.Migrator().HasColumn(Bounty{}, "workspace_uuid") {
db.db.AutoMigrate(&Bounty{})
}
}
if !db.db.Migrator().HasTable("budget_histories") {
if !db.db.Migrator().HasColumn(BudgetHistory{}, "workspace_uuid") {
db.db.AutoMigrate(&BudgetHistory{})
}
}
if !db.db.Migrator().HasTable("payment_histories") {
if !db.db.Migrator().HasColumn(PaymentHistory{}, "workspace_uuid") {
db.db.AutoMigrate(&PaymentHistory{})
}
}
if !db.db.Migrator().HasTable("invoice_list") {
if !db.db.Migrator().HasColumn(InvoiceList{}, "workspace_uuid") {
db.db.AutoMigrate(&InvoiceList{})
}
}
if !db.db.Migrator().HasTable("bounty_budgets") {
if !db.db.Migrator().HasColumn(BountyBudget{}, "workspace_uuid") {
db.db.AutoMigrate(&BountyBudget{})
}
}
if !db.db.Migrator().HasTable("workspace_user_roles") {
if !db.db.Migrator().HasColumn(UserRoles{}, "workspace_uuid") {
db.db.AutoMigrate(&UserRoles{})
}
}
if !db.db.Migrator().HasTable("workspaces") {
db.db.AutoMigrate(&Organization{})
}
if !db.db.Migrator().HasTable("workspace_users") {
db.db.AutoMigrate(&OrganizationUsers{})
}
}

func (db database) MigrateOrganizationToWorkspace() {
if (db.db.Migrator().HasTable(&Organization{}) && !db.db.Migrator().HasTable("workspaces")) {
db.db.Migrator().RenameTable(&Organization{}, "workspaces")
}

if (db.db.Migrator().HasTable(&OrganizationUsers{}) && !db.db.Migrator().HasTable("workspace_users")) {
if db.db.Migrator().HasColumn(&OrganizationUsers{}, "org_uuid") {
db.db.Migrator().RenameColumn(&OrganizationUsers{}, "org_uuid", "workspace_uuid")
}
db.db.Migrator().RenameTable(&OrganizationUsers{}, "workspace_users")
}

if (db.db.Migrator().HasTable(&UserRoles{}) && !db.db.Migrator().HasTable("workspace_user_roles")) {
if db.db.Migrator().HasColumn(&UserRoles{}, "org_uuid") {
db.db.Migrator().RenameColumn(&UserRoles{}, "org_uuid", "workspace_uuid")
}

db.db.Migrator().RenameTable(&UserRoles{}, "workspace_user_roles")
}

if (db.db.Migrator().HasTable(&Bounty{})) {
if db.db.Migrator().HasColumn(&Bounty{}, "org_uuid") {
db.db.Migrator().RenameColumn(&Bounty{}, "org_uuid", "workspace_uuid")
}
}

if (db.db.Migrator().HasTable(&BountyBudget{})) {
if db.db.Migrator().HasColumn(&BountyBudget{}, "org_uuid") {
db.db.Migrator().RenameColumn(&BountyBudget{}, "org_uuid", "workspace_uuid")
}
}

if (db.db.Migrator().HasTable(&BudgetHistory{})) {
if db.db.Migrator().HasColumn(&BudgetHistory{}, "org_uuid") {
db.db.Migrator().RenameColumn(&BudgetHistory{}, "org_uuid", "workspace_uuid")
}
}

if (db.db.Migrator().HasTable(&PaymentHistory{})) {
if db.db.Migrator().HasColumn(&PaymentHistory{}, "org_uuid") {
db.db.Migrator().RenameColumn(&PaymentHistory{}, "org_uuid", "workspace_uuid")
}
}

if (db.db.Migrator().HasTable(&InvoiceList{})) {
if db.db.Migrator().HasColumn(&InvoiceList{}, "org_uuid") {
db.db.Migrator().RenameColumn(&InvoiceList{}, "org_uuid", "workspace_uuid")
}
}
}

func (db database) CreateRoles() {
db.db.Create(&ConfigBountyRoles)
}
Expand Down Expand Up @@ -205,7 +290,7 @@ func GetRolesMap() map[string]string {
return roles
}

func GetUserRolesMap(userRoles []UserRoles) map[string]string {
func GetUserRolesMap(userRoles []WorkspaceUserRoles) map[string]string {
roles := map[string]string{}
for _, v := range userRoles {
roles[v.Role] = v.Role
Expand Down Expand Up @@ -235,7 +320,7 @@ func (db database) ConvertMetricsBountiesToMap(metricsCsv []MetricsBountyCsv) []
return metricsMap
}

func RolesCheck(userRoles []UserRoles, check string) bool {
func RolesCheck(userRoles []WorkspaceUserRoles, check string) bool {
rolesMap := GetRolesMap()
userRolesMap := GetUserRolesMap(userRoles)

Expand All @@ -253,7 +338,7 @@ func RolesCheck(userRoles []UserRoles, check string) bool {
return true
}

func CheckUser(userRoles []UserRoles, pubkey string) bool {
func CheckUser(userRoles []WorkspaceUserRoles, pubkey string) bool {
for _, role := range userRoles {
if role.OwnerPubKey == pubkey {
return true
Expand Down
66 changes: 33 additions & 33 deletions db/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

func TestRolesCheck_UserHasRole(t *testing.T) {
// Mock user roles
userRoles := []UserRoles{
{Role: "ADD BOUNTY", OwnerPubKey: "user1", OrgUuid: "org1", Created: &time.Time{}},
userRoles := []WorkspaceUserRoles{
{Role: "ADD BOUNTY", OwnerPubKey: "user1", WorkspaceUuid: "org1", Created: &time.Time{}},
}

// Role to check
Expand All @@ -28,8 +28,8 @@ func TestRolesCheck_UserHasRole(t *testing.T) {

func TestRolesCheck_UserDoesNotHaveRole(t *testing.T) {
// Mock user roles
userRoles := []UserRoles{
{Role: "DELETE BOUNTY", OwnerPubKey: "user2", OrgUuid: "org1", Created: &time.Time{}},
userRoles := []WorkspaceUserRoles{
{Role: "DELETE BOUNTY", OwnerPubKey: "user2", WorkspaceUuid: "org1", Created: &time.Time{}},
}

// Role to check
Expand All @@ -45,7 +45,7 @@ func TestRolesCheck_UserDoesNotHaveRole(t *testing.T) {
}

func TestCheckUser(t *testing.T) {
userRoles := []UserRoles{
userRoles := []WorkspaceUserRoles{
{OwnerPubKey: "userPublicKey"},
}

Expand All @@ -55,16 +55,16 @@ func TestCheckUser(t *testing.T) {
}

func TestUserHasAccess(t *testing.T) {
mockGetWorkspaceByUuid := func(uuid string) Organization {
return Organization{
mockGetWorkspaceByUuid := func(uuid string) Workspace {
return Workspace{
Uuid: uuid,
OwnerPubKey: "org_admin",
}
}

mockGetUserRoles := func(uuid string, pubkey string) []UserRoles {
return []UserRoles{
{Role: "ADD BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}},
mockGetUserRoles := func(uuid string, pubkey string) []WorkspaceUserRoles {
return []WorkspaceUserRoles{
{Role: "ADD BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}},
}
}

Expand All @@ -74,26 +74,26 @@ func TestUserHasAccess(t *testing.T) {
databaseConfig.getWorkspaceByUuid = mockGetWorkspaceByUuid
databaseConfig.getUserRoles = mockGetUserRoles

t.Run("Should test that if the user is the admin of an organization returns true", func(t *testing.T) {
result := databaseConfig.UserHasAccess("org_admin", "org_uuid", "ADD BOUNTY")
t.Run("Should test that if the user is the admin of an workspace returns true", func(t *testing.T) {
result := databaseConfig.UserHasAccess("org_admin", "workspace_uuid", "ADD BOUNTY")

// Assert that it returns true since the user is the org admin
if !result {
t.Errorf("Expected UserHasAccess to return true for organization admin, got false")
t.Errorf("Expected UserHasAccess to return true for workspace admin, got false")
}
})

t.Run("Should test that if the user is not the organization admin, and the user has the required role it should return true", func(t *testing.T) {
result := databaseConfig.UserHasAccess("user_pubkey", "org_uuid", "ADD BOUNTY")
t.Run("Should test that if the user is not the workspace admin, and the user has the required role it should return true", func(t *testing.T) {
result := databaseConfig.UserHasAccess("user_pubkey", "workspace_uuid", "ADD BOUNTY")

// Assert that it returns true since the user has the required role
if !result {
t.Errorf("Expected UserHasAccess to return true for user with required role, got false")
}
})

t.Run("Should test that if the user is not the organization admin, and the user has not the required role it should return false", func(t *testing.T) {
result := databaseConfig.UserHasAccess("user_pubkey", "org_uuid", "DELETE BOUNTY")
t.Run("Should test that if the user is not the workspace admin, and the user has not the required role it should return false", func(t *testing.T) {
result := databaseConfig.UserHasAccess("user_pubkey", "workspace_uuid", "DELETE BOUNTY")

// Assert that it returns false since the user does not have the required role
if result {
Expand All @@ -103,24 +103,24 @@ func TestUserHasAccess(t *testing.T) {
}

func TestUserHasManageBountyRoles(t *testing.T) {
mockGetWorkspaceByUuid := func(uuid string) Organization {
return Organization{
mockGetWorkspaceByUuid := func(uuid string) Workspace {
return Workspace{
Uuid: uuid,
OwnerPubKey: "org_admin",
}
}

mockGetUserRoles := func(uuid string, pubkey string) []UserRoles {
if uuid == "org_uuid" {
return []UserRoles{
{Role: "ADD BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}},
mockGetUserRoles := func(uuid string, pubkey string) []WorkspaceUserRoles {
if uuid == "workspace_uuid" {
return []WorkspaceUserRoles{
{Role: "ADD BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}},
}
} else {
return []UserRoles{
{Role: "ADD BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}},
{Role: "UPDATE BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}},
{Role: "DELETE BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}},
{Role: "PAY BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}},
return []WorkspaceUserRoles{
{Role: "ADD BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}},
{Role: "UPDATE BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}},
{Role: "DELETE BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}},
{Role: "PAY BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}},
}
}
}
Expand All @@ -131,22 +131,22 @@ func TestUserHasManageBountyRoles(t *testing.T) {
databaseConfig.getWorkspaceByUuid = mockGetWorkspaceByUuid
databaseConfig.getUserRoles = mockGetUserRoles

t.Run("Should test that if the user is the organization admin return true", func(t *testing.T) {
result := databaseConfig.UserHasManageBountyRoles("org_admin", "org_uuid")
t.Run("Should test that if the user is the workspace admin return true", func(t *testing.T) {
result := databaseConfig.UserHasManageBountyRoles("org_admin", "workspace_uuid")

// Assert that it returns true since the user is the org admin
assert.True(t, result, "Expected UserHasManageBountyRoles to return true for organization admin")
assert.True(t, result, "Expected UserHasManageBountyRoles to return true for workspace admin")
})

t.Run("Should test that if the user has all bounty roles return true", func(t *testing.T) {
result := databaseConfig.UserHasManageBountyRoles("user_pubkey", "org_uuid2")
result := databaseConfig.UserHasManageBountyRoles("user_pubkey", "workspace_uuid2")

// Assert that it returns true since the user has all bounty roles
assert.True(t, result, "Expected UserHasManageBountyRoles to return true for user with all bounty roles")
})

t.Run("Should test that if the user don't have all bounty roles return false.", func(t *testing.T) {
result := databaseConfig.UserHasManageBountyRoles("user_pubkey", "org_uuid")
result := databaseConfig.UserHasManageBountyRoles("user_pubkey", "workspace_uuid")

// Assert that it returns false since the user does not have all bounty roles
assert.False(t, result, "Expected UserHasManageBountyRoles to return false for user without all bounty roles")
Expand Down
Loading

0 comments on commit 7667276

Please sign in to comment.