eintopf.info@v0.13.16/service/place/place_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 place_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/place"
    29  )
    30  
    31  func TestPlaceIndexable(t *testing.T) {
    32  	for i, test := range []struct {
    33  		place     *place.Place
    34  		indexable bool
    35  	}{
    36  		{
    37  			place:     &place.Place{Deactivated: true, Published: true},
    38  			indexable: false,
    39  		}, {
    40  			place:     &place.Place{Deactivated: false, Published: false},
    41  			indexable: true,
    42  		}, {
    43  			place:     &place.Place{Deactivated: false, Published: true},
    44  			indexable: true,
    45  		},
    46  	} {
    47  		t.Run(fmt.Sprintf("%d", i), func(tt *testing.T) {
    48  			if test.place.Indexable() != test.indexable {
    49  				tt.Errorf("%d: Deactivated: %t Published: %t -> Indexable %t", i, test.place.Deactivated, test.place.Published, test.place.Indexable())
    50  			}
    51  		})
    52  	}
    53  }
    54  
    55  func TestPlaceListable(t *testing.T) {
    56  	for i, test := range []struct {
    57  		place    *place.Place
    58  		listable bool
    59  	}{
    60  		{
    61  			place:    &place.Place{Deactivated: true, Published: true},
    62  			listable: true,
    63  		}, {
    64  			place:    &place.Place{Deactivated: false, Published: false},
    65  			listable: false,
    66  		}, {
    67  			place:    &place.Place{Deactivated: false, Published: true},
    68  			listable: true,
    69  		},
    70  	} {
    71  		t.Run(fmt.Sprintf("%d", i), func(tt *testing.T) {
    72  			if test.place.Listable() != test.listable {
    73  				tt.Errorf("%d: Deactivated: %t Published: %t -> Listable %t", i, test.place.Deactivated, test.place.Published, test.place.Listable())
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestPlaceIsOwned(t *testing.T) {
    80  	for _, test := range []struct {
    81  		name  string
    82  		place *place.Place
    83  		id    string
    84  		owned bool
    85  	}{
    86  		{
    87  			name:  "Owned",
    88  			place: &place.Place{OwnedBy: []string{"foo", "bar"}},
    89  			id:    "foo",
    90  			owned: true,
    91  		}, {
    92  			name:  "NotOwned",
    93  			place: &place.Place{OwnedBy: []string{"bar"}},
    94  			id:    "foo",
    95  			owned: false,
    96  		},
    97  	} {
    98  		t.Run(test.name, func(tt *testing.T) {
    99  			if test.place.IsOwned(test.id) != test.owned {
   100  				tt.Errorf("test.place.IsOwned(%s) != test.owned: %t != %t", test.id, test.place.IsOwned(test.id), test.owned)
   101  			}
   102  		})
   103  	}
   104  }
   105  
   106  func TestNewPlaceIsOwned(t *testing.T) {
   107  	for _, test := range []struct {
   108  		name  string
   109  		place *place.NewPlace
   110  		id    string
   111  		owned bool
   112  	}{
   113  		{
   114  			name:  "Owned",
   115  			place: &place.NewPlace{OwnedBy: []string{"foo", "bar"}},
   116  			id:    "foo",
   117  			owned: true,
   118  		}, {
   119  			name:  "NotOwned",
   120  			place: &place.NewPlace{OwnedBy: []string{"bar"}},
   121  			id:    "foo",
   122  			owned: false,
   123  		},
   124  	} {
   125  		t.Run(test.name, func(tt *testing.T) {
   126  			if test.place.IsOwned(test.id) != test.owned {
   127  				tt.Errorf("test.place.IsOwned(%s) != test.owned: %t != %t", test.id, test.place.IsOwned(test.id), test.owned)
   128  			}
   129  		})
   130  	}
   131  }
   132  
   133  func TestServiceCreateAddsOwnedBy(t *testing.T) {
   134  	pegomock.RegisterMockTestingT(t)
   135  
   136  	mockedStore := mock.NewPlaceStore()
   137  	service := place.NewService(mockedStore)
   138  
   139  	g := &place.NewPlace{OwnedBy: []string{"foo"}}
   140  	ctx := auth.ContextWithID(context.Background(), "bar")
   141  	service.Create(ctx, g)
   142  
   143  	mockedStore.VerifyWasCalledOnce().Create(ctx, &place.NewPlace{
   144  		OwnedBy: []string{"foo", "bar"},
   145  	})
   146  }
   147  
   148  func TestServiceCreateWhiteSpace(t *testing.T) {
   149  	store := place.NewMemoryStore()
   150  	service := place.NewService(store)
   151  
   152  	ctx := auth.ContextWithID(context.Background(), "bar")
   153  	p, _ := service.Create(ctx, &place.NewPlace{Name: " foo "})
   154  	if p.Name != "foo" {
   155  		t.Fatalf("should remove white space from name: '%s'", p.Name)
   156  	}
   157  }
   158  
   159  func TestServiceFindFilterOwnedBySelf(t *testing.T) {
   160  	pegomock.RegisterMockTestingT(t)
   161  
   162  	mockedStore := mock.NewPlaceStore()
   163  	service := place.NewService(mockedStore)
   164  
   165  	ctx := auth.ContextWithID(context.Background(), "bar")
   166  	service.Find(ctx, &crud.FindParams[place.FindFilters]{
   167  		Filters: &place.FindFilters{OwnedBy: []string{"self"}},
   168  	})
   169  
   170  	mockedStore.VerifyWasCalledOnce().Find(ctx, &crud.FindParams[place.FindFilters]{
   171  		Filters: &place.FindFilters{OwnedBy: []string{"bar"}},
   172  	})
   173  }