|
| 1 | +package organization |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/documize/community/core/uniqueid" |
| 7 | + "github.com/documize/community/model/org" |
| 8 | + |
| 9 | + "github.com/documize/community/domain/test" |
| 10 | +) |
| 11 | + |
| 12 | +// TestSpace tests all space database operations. |
| 13 | + |
| 14 | +func TestOrganization(t *testing.T) { |
| 15 | + rt, s, ctx := test.SetupTest() |
| 16 | + //Create a new organization |
| 17 | + var err error |
| 18 | + org := org.Organization{} |
| 19 | + orgID := uniqueid.Generate() |
| 20 | + |
| 21 | + t.Run("AddOrginization", func(t *testing.T) { |
| 22 | + ctx.Transaction, err = rt.Db.Beginx() |
| 23 | + if err != nil { |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + org.RefID = orgID |
| 28 | + org.Company = "test" |
| 29 | + org.Title = "test" |
| 30 | + org.Message = "test" |
| 31 | + org.Domain = "testDomain" |
| 32 | + org.Active = true |
| 33 | + |
| 34 | + err = s.Organization.AddOrganization(ctx, org) |
| 35 | + if err != nil { |
| 36 | + ctx.Transaction.Rollback() |
| 37 | + t.Error("failed to add org organization") |
| 38 | + } |
| 39 | + |
| 40 | + ctx.Transaction.Commit() |
| 41 | + |
| 42 | + orgGot, err := s.Organization.GetOrganization(ctx, org.RefID) |
| 43 | + if err != nil || org.Title != orgGot.Title { |
| 44 | + t.Error("failed to get org organization") |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("GetOrganizationByDomain", func(t *testing.T) { |
| 49 | + orgGot, err := s.Organization.GetOrganizationByDomain("testDomain") |
| 50 | + if err != nil || org.Title != orgGot.Title { |
| 51 | + t.Error("failed to get org organization by domain") |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("UpdateOrginization", func(t *testing.T) { |
| 56 | + ctx.Transaction, err = rt.Db.Beginx() |
| 57 | + if err != nil { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + org.Title = "testUpdate" |
| 62 | + |
| 63 | + err = s.Organization.UpdateOrganization(ctx, org) |
| 64 | + if err != nil { |
| 65 | + ctx.Transaction.Rollback() |
| 66 | + t.Error("failed to update org organization") |
| 67 | + } |
| 68 | + |
| 69 | + ctx.Transaction.Commit() |
| 70 | + |
| 71 | + orgGot, err := s.Organization.GetOrganization(ctx, org.RefID) |
| 72 | + if err != nil || org.Title != orgGot.Title { |
| 73 | + t.Error("failed to get updated org organization") |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("CheckDomain", func(t *testing.T) { |
| 78 | + Domain := s.Organization.CheckDomain(ctx, "") |
| 79 | + if Domain != Domain { |
| 80 | + t.Error("failed to CheckDomain") |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("UpdateAuthConfig", func(t *testing.T) { |
| 85 | + ctx.Transaction, err = rt.Db.Beginx() |
| 86 | + if err != nil { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + err = s.Organization.UpdateAuthConfig(ctx, org) |
| 91 | + if err != nil { |
| 92 | + ctx.Transaction.Rollback() |
| 93 | + t.Error("failed to update organization AuthConfig") |
| 94 | + } |
| 95 | + |
| 96 | + ctx.Transaction.Commit() |
| 97 | + }) |
| 98 | + |
| 99 | + // |
| 100 | + //Run after everything except delete as this makes an org inactive |
| 101 | + // |
| 102 | + |
| 103 | + t.Run("RemoveOrganization", func(t *testing.T) { |
| 104 | + ctx.Transaction, err = rt.Db.Beginx() |
| 105 | + if err != nil { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + err = s.Organization.RemoveOrganization(ctx, org.RefID) |
| 110 | + if err != nil { |
| 111 | + ctx.Transaction.Rollback() |
| 112 | + t.Error("failed to remove organization") |
| 113 | + } |
| 114 | + |
| 115 | + ctx.Transaction.Commit() |
| 116 | + |
| 117 | + orgGot, err := s.Organization.GetOrganization(ctx, org.RefID) |
| 118 | + if err != nil || orgGot.Active != false { |
| 119 | + t.Error("failed to get removed organization activity") |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + // |
| 124 | + // teardown code goes here |
| 125 | + // |
| 126 | + |
| 127 | + t.Run("DeleteOrganization", func(t *testing.T) { |
| 128 | + ctx.Transaction, err = rt.Db.Beginx() |
| 129 | + if err != nil { |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + _, |
| 134 | + err = s.Organization.DeleteOrganization(ctx, orgID) |
| 135 | + if err != nil { |
| 136 | + ctx.Transaction.Rollback() |
| 137 | + t.Error("failed to delete org organization") |
| 138 | + } |
| 139 | + |
| 140 | + ctx.Transaction.Commit() |
| 141 | + }) |
| 142 | +} |
0 commit comments