eintopf.info@v0.13.16/service/event/event_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 event_test 17 18 import ( 19 "context" 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 24 "eintopf.info/internal/crud" 25 "eintopf.info/service/auth" 26 "eintopf.info/service/dbmigration" 27 "eintopf.info/service/event" 28 "eintopf.info/test" 29 ) 30 31 func TestEventIndexable(t *testing.T) { 32 tests := []struct { 33 event *event.Event 34 indexable bool 35 }{ 36 {event: &event.Event{Deactivated: true, Published: true}, indexable: false}, 37 {event: &event.Event{Published: false}, indexable: true}, 38 {event: &event.Event{Published: true}, indexable: true}, 39 {event: &event.Event{Published: true, Parent: "foo"}, indexable: true}, 40 {event: &event.Event{Published: true, Parent: "foo", ParentListed: true}, indexable: true}, 41 } 42 43 for i, test := range tests { 44 if test.event.Indexable() != test.indexable { 45 t.Errorf( 46 "%d: Deactivated: %t, Published: %t, Parent: %s, ParentListed: %t -> Indexable %t", 47 i, 48 test.event.Deactivated, 49 test.event.Published, 50 test.event.Parent, 51 test.event.ParentListed, 52 test.indexable, 53 ) 54 } 55 } 56 } 57 58 func TestEventListable(t *testing.T) { 59 tests := []struct { 60 event *event.Event 61 listable bool 62 }{ 63 {event: &event.Event{Deactivated: true, Published: true}, listable: true}, 64 {event: &event.Event{Published: false}, listable: false}, 65 {event: &event.Event{Published: true}, listable: true}, 66 {event: &event.Event{Published: true, Parent: "foo", ParentListed: false}, listable: false}, 67 {event: &event.Event{Published: true, Parent: "foo", ParentListed: true}, listable: true}, 68 {event: &event.Event{Published: true, Parent: "revent:foo"}, listable: true}, 69 } 70 71 for i, test := range tests { 72 if test.event.Listable() != test.listable { 73 t.Errorf( 74 "%d: Deactivated: %t, Published: %t, Parent: %s, ParentListed: %t -> Listable %t", 75 i, 76 test.event.Deactivated, 77 test.event.Published, 78 test.event.Parent, 79 test.event.ParentListed, 80 test.listable, 81 ) 82 } 83 } 84 } 85 86 func TestEventServiceCreateOwnedBy(t *testing.T) { 87 store := event.NewMemoryStore() 88 eventService := event.NewService(store, groupOwnerService{}) 89 90 // It adds the OwnedBy field. 91 e, err := eventService.Create(auth.ContextWithID(context.Background(), "foo"), &event.NewEvent{}) 92 if err != nil { 93 t.Fatalf("expected eventService.Create to succeed. got %s", err.Error()) 94 } 95 if e.OwnedBy[0] != "foo" { 96 t.Fatal("OwnedBy should be set") 97 } 98 } 99 100 func TestEventServiceWhiteSpace(t *testing.T) { 101 store := event.NewMemoryStore() 102 eventService := event.NewService(store, groupOwnerService{}) 103 104 // It adds the OwnedBy field. 105 e, err := eventService.Create(auth.ContextWithID(context.Background(), "foo"), &event.NewEvent{Name: " foo "}) 106 if err != nil { 107 t.Fatalf("expected eventService.Create to succeed. got %s", err.Error()) 108 } 109 if e.Name != "foo" { 110 t.Fatalf("should remove white space from name: '%s'", e.Name) 111 } 112 } 113 114 func TestEventServiceFindOwnedBySelf(t *testing.T) { 115 db, cleanup, _ := test.CreateSqliteTestDB("TestEventServiceFindOwnedBySelf") 116 t.Cleanup(cleanup) 117 migration, _ := dbmigration.NewSqlStore(db) 118 119 store, _ := event.NewSqlStore(db, dbmigration.NewService(migration)) 120 e1, err := store.Create(context.Background(), &event.NewEvent{OwnedBy: []string{"1"}, Tags: []string{}, Involved: []event.Involved{}, Organizers: []string{}}) 121 if err != nil { 122 t.Fatal(err) 123 } 124 125 eventService := event.NewService(store, groupOwnerService{}) 126 127 ctx := auth.ContextWithID(context.Background(), "1") 128 events, total, err := eventService.Find(ctx, &crud.FindParams[event.FindFilters]{Filters: &event.FindFilters{OwnedBy: []string{"self"}}}) 129 if err != nil { 130 t.Fatal(err) 131 } 132 if total != 1 { 133 t.Errorf("total mismatch: want: %d, got: %d", 1, total) 134 } 135 if diff := cmp.Diff([]*event.Event{e1}, events); diff != "" { 136 t.Errorf("events mismatch (-want +got):\n%s", diff) 137 } 138 } 139 140 func TestEventServiceFindOwnedByGroup(t *testing.T) { 141 db, cleanup, _ := test.CreateSqliteTestDB("TestEventServiceFindOwnedByGroup") 142 t.Cleanup(cleanup) 143 migration, _ := dbmigration.NewSqlStore(db) 144 145 store, _ := event.NewSqlStore(db, dbmigration.NewService(migration)) 146 _, err := store.Create(context.Background(), &event.NewEvent{OwnedBy: []string{"1"}}) 147 if err != nil { 148 t.Fatal(err) 149 } 150 e2, err := store.Create(context.Background(), &event.NewEvent{ 151 Name: "a", 152 OwnedBy: []string{"1"}, 153 Organizers: []string{"id:3", "id:2"}, 154 Tags: []string{}, 155 Involved: []event.Involved{}, 156 }) 157 if err != nil { 158 t.Fatal(err) 159 } 160 e3, err := store.Create(context.Background(), &event.NewEvent{ 161 Name: "b", 162 OwnedBy: []string{"1"}, 163 Organizers: []string{"id:2"}, 164 Tags: []string{}, 165 Involved: []event.Involved{}, 166 }) 167 if err != nil { 168 t.Fatal(err) 169 } 170 171 eventService := event.NewService(store, groupOwnerService{}) 172 173 events, total, err := eventService.Find(context.Background(), &crud.FindParams[event.FindFilters]{ 174 Sort: "name", 175 Filters: &event.FindFilters{OwnedBy: []string{"2"}}, 176 }) 177 if err != nil { 178 t.Fatal(err) 179 } 180 if total != 2 { 181 t.Errorf("total mismatch: want: %d, got: %d", 2, total) 182 } 183 if diff := cmp.Diff([]*event.Event{e2, e3}, events); diff != "" { 184 t.Errorf("events mismatch (-want +got):\n%s", diff) 185 } 186 } 187 188 type groupOwnerService struct{} 189 190 // GroupIDsByOwners returns the following: 191 // 192 // 1 -> 1, 2 193 // 2 -> 3, 4 194 // 3 -> 4 195 func (g groupOwnerService) GroupIDsByOwners(ctx context.Context, ownedBy []string) ([]string, error) { 196 for _, o := range ownedBy { 197 switch o { 198 case "1": 199 return []string{"1", "2"}, nil 200 case "2": 201 return []string{"2", "3"}, nil 202 case "3": 203 return []string{"4"}, nil 204 } 205 } 206 return []string{}, nil 207 }