eintopf.info@v0.13.16/service/group/group_test.go (about) 1 // Copyright (C) 2022 The Eintopf authors 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 16 package group_test 17 18 import ( 19 "context" 20 "fmt" 21 "testing" 22 23 "github.com/petergtz/pegomock" 24 25 "eintopf.info/internal/crud" 26 "eintopf.info/internal/mock" 27 "eintopf.info/service/auth" 28 "eintopf.info/service/group" 29 ) 30 31 func TestGroupIndexable(t *testing.T) { 32 for i, test := range []struct { 33 group *group.Group 34 indexable bool 35 }{ 36 { 37 group: &group.Group{Deactivated: true, Published: true}, 38 indexable: false, 39 }, { 40 group: &group.Group{Deactivated: false, Published: true}, 41 indexable: true, 42 }, { 43 group: &group.Group{Deactivated: false, Published: false}, 44 indexable: true, 45 }, 46 } { 47 t.Run(fmt.Sprintf("%d", i), func(tt *testing.T) { 48 if test.group.Indexable() != test.indexable { 49 tt.Errorf("%d: Deactivated: %t Published: %t -> Indexable %t", i, test.group.Deactivated, test.group.Published, test.group.Indexable()) 50 } 51 }) 52 } 53 } 54 55 func TestGroupListable(t *testing.T) { 56 for i, test := range []struct { 57 group *group.Group 58 listable bool 59 }{ 60 { 61 group: &group.Group{Deactivated: true, Published: true}, 62 listable: true, 63 }, { 64 group: &group.Group{Deactivated: false, Published: true}, 65 listable: true, 66 }, { 67 group: &group.Group{Deactivated: false, Published: false}, 68 listable: false, 69 }, 70 } { 71 t.Run(fmt.Sprintf("%d", i), func(tt *testing.T) { 72 if test.group.Listable() != test.listable { 73 tt.Errorf("%d: Deactivated: %t Published: %t -> Listable %t", i, test.group.Deactivated, test.group.Published, test.group.Listable()) 74 } 75 }) 76 } 77 } 78 79 func TestGroupIsOwned(t *testing.T) { 80 for _, test := range []struct { 81 name string 82 group *group.Group 83 id string 84 owned bool 85 }{ 86 { 87 name: "Owned", 88 group: &group.Group{OwnedBy: []string{"foo", "bar"}}, 89 id: "foo", 90 owned: true, 91 }, { 92 name: "NotOwned", 93 group: &group.Group{OwnedBy: []string{"bar"}}, 94 id: "foo", 95 owned: false, 96 }, 97 } { 98 t.Run(test.name, func(tt *testing.T) { 99 if test.group.IsOwned(test.id) != test.owned { 100 tt.Errorf("test.group.IsOwned(%s) != test.owned: %t != %t", test.id, test.group.IsOwned(test.id), test.owned) 101 } 102 }) 103 } 104 } 105 106 func TestNewGroupIsOwned(t *testing.T) { 107 for _, test := range []struct { 108 name string 109 group *group.NewGroup 110 id string 111 owned bool 112 }{ 113 { 114 name: "Owned", 115 group: &group.NewGroup{OwnedBy: []string{"foo", "bar"}}, 116 id: "foo", 117 owned: true, 118 }, { 119 name: "NotOwned", 120 group: &group.NewGroup{OwnedBy: []string{"bar"}}, 121 id: "foo", 122 owned: false, 123 }, 124 } { 125 t.Run(test.name, func(tt *testing.T) { 126 if test.group.IsOwned(test.id) != test.owned { 127 tt.Errorf("test.group.IsOwned(%s) != test.owned: %t != %t", test.id, test.group.IsOwned(test.id), test.owned) 128 } 129 }) 130 } 131 } 132 133 func TestServiceCreateAddsOwnedBy(t *testing.T) { 134 pegomock.RegisterMockTestingT(t) 135 136 mockedStore := mock.NewGroupStore() 137 service := group.NewService(mockedStore) 138 139 g := &group.NewGroup{OwnedBy: []string{"foo"}} 140 ctx := auth.ContextWithID(context.Background(), "bar") 141 service.Create(ctx, g) 142 143 mockedStore.VerifyWasCalledOnce().Create(ctx, &group.NewGroup{ 144 OwnedBy: []string{"foo", "bar"}, 145 }) 146 } 147 148 func TestServiceCreateWhiteSpace(t *testing.T) { 149 store := group.NewMemoryStore() 150 service := group.NewService(store) 151 152 ctx := auth.ContextWithID(context.Background(), "bar") 153 g, _ := service.Create(ctx, &group.NewGroup{Name: " foo "}) 154 155 if g.Name != "foo" { 156 t.Fatalf("should remove white space from name: '%s'", g.Name) 157 } 158 } 159 160 func TestServiceFindFilterOwnedBySelf(t *testing.T) { 161 pegomock.RegisterMockTestingT(t) 162 163 mockedStore := mock.NewGroupStore() 164 service := group.NewService(mockedStore) 165 166 ctx := auth.ContextWithID(context.Background(), "bar") 167 service.Find(ctx, &crud.FindParams[group.FindFilters]{ 168 Filters: &group.FindFilters{OwnedBy: []string{"self"}}, 169 }) 170 171 mockedStore.VerifyWasCalledOnce().Find(ctx, &crud.FindParams[group.FindFilters]{ 172 Filters: &group.FindFilters{OwnedBy: []string{"bar"}}, 173 }) 174 }