eintopf.info@v0.13.16/service/notification/authorizer_test.go (about)

     1  // Copyright (C) 2024 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 notification_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"eintopf.info/service/auth"
    23  	"eintopf.info/service/notification"
    24  )
    25  
    26  var (
    27  	normalCtx    = auth.ContextWithRole(context.Background(), auth.RoleNormal)
    28  	moderatorCtx = auth.ContextWithRole(context.Background(), auth.RoleModerator)
    29  	adminCtx     = auth.ContextWithRole(context.Background(), auth.RoleAdmin)
    30  	internalCtx  = auth.ContextWithRole(context.Background(), auth.RoleInternal)
    31  )
    32  
    33  func TestAuthorizerCreate(t *testing.T) {
    34  	store := notification.NewMemoryStore()
    35  	authorizer := notification.NewAuthorizer(store)
    36  
    37  	t.Run("Background context -> Unauthorized", func(t *testing.T) {
    38  		_, err := authorizer.Create(context.Background(), &notification.NewNotification{})
    39  		if err == nil {
    40  			t.Error("should return unauthorized error")
    41  		}
    42  	})
    43  
    44  	t.Run("Normal -> Unauthorized", func(t *testing.T) {
    45  		_, err := authorizer.Create(normalCtx, &notification.NewNotification{})
    46  		if err == nil {
    47  			t.Error("should return unauthorized error")
    48  		}
    49  	})
    50  
    51  	t.Run("Moderator -> Authorized", func(t *testing.T) {
    52  		_, err := authorizer.Create(moderatorCtx, &notification.NewNotification{})
    53  		if err != nil {
    54  			t.Error("should not return unauthorized error")
    55  		}
    56  	})
    57  
    58  	t.Run("Admin -> Authorized", func(t *testing.T) {
    59  		_, err := authorizer.Create(adminCtx, &notification.NewNotification{})
    60  		if err != nil {
    61  			t.Error("should not return unauthorized error")
    62  		}
    63  	})
    64  
    65  	t.Run("Internal -> Authorized", func(t *testing.T) {
    66  		_, err := authorizer.Create(internalCtx, &notification.NewNotification{})
    67  		if err != nil {
    68  			t.Error("should not return unauthorized error")
    69  		}
    70  	})
    71  }
    72  
    73  func TestAuthorizerUpdate(t *testing.T) {
    74  	store := notification.NewMemoryStore()
    75  	_, err := store.Create(context.Background(), &notification.NewNotification{
    76  		UserID:  "1312",
    77  		Message: "all cats are beautifull",
    78  	})
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	authorizer := notification.NewAuthorizer(store)
    83  
    84  	t.Run("Background -> Unauthorized", func(t *testing.T) {
    85  		_, err := authorizer.Update(context.Background(), &notification.Notification{})
    86  		if err == nil {
    87  			t.Error("should return unauthorized error")
    88  		}
    89  	})
    90  
    91  	t.Run("Different Normal user -> Unauthorized", func(t *testing.T) {
    92  		_, err := authorizer.Update(auth.ContextWithID(normalCtx, "foo"), &notification.Notification{
    93  			ID:     "0",
    94  			UserID: "1312",
    95  			Viewed: true,
    96  		})
    97  		if err == nil {
    98  			t.Error("should return unauthorized error")
    99  		}
   100  	})
   101  
   102  	t.Run("Same Normal user -> Authorized", func(t *testing.T) {
   103  		_, err := authorizer.Update(auth.ContextWithID(normalCtx, "1312"), &notification.Notification{
   104  			ID:     "0",
   105  			UserID: "1312",
   106  			Viewed: true,
   107  		})
   108  		if err != nil {
   109  			t.Error("should not return unauthorized error")
   110  		}
   111  	})
   112  
   113  	t.Run("Different Moderator -> Unauthorized", func(t *testing.T) {
   114  		_, err := authorizer.Update(auth.ContextWithID(moderatorCtx, "foo"), &notification.Notification{
   115  			ID:     "0",
   116  			UserID: "1312",
   117  			Viewed: true,
   118  		})
   119  		if err == nil {
   120  			t.Error("should return unauthorized error")
   121  		}
   122  	})
   123  
   124  	t.Run("Same Moderator -> Authorized", func(t *testing.T) {
   125  		_, err := authorizer.Update(auth.ContextWithID(moderatorCtx, "1312"), &notification.Notification{
   126  			ID:     "0",
   127  			UserID: "1312",
   128  			Viewed: true,
   129  		})
   130  		if err != nil {
   131  			t.Error("should not return unauthorized error")
   132  		}
   133  	})
   134  
   135  	t.Run("Admin -> Authorized", func(t *testing.T) {
   136  		_, err := authorizer.Update(adminCtx, &notification.Notification{ID: "0"})
   137  		if err != nil {
   138  			t.Error("should not return unauthorized error")
   139  		}
   140  	})
   141  
   142  	t.Run("Internal -> Authorized", func(t *testing.T) {
   143  		_, err := authorizer.Update(internalCtx, &notification.Notification{ID: "0"})
   144  		if err != nil {
   145  			t.Error("should not return unauthorized error")
   146  		}
   147  	})
   148  }
   149  
   150  func TestAuthorizerDelete(t *testing.T) {
   151  	store := notification.NewMemoryStore()
   152  	_, err := store.Create(context.Background(), &notification.NewNotification{
   153  		UserID:  "1312",
   154  		Message: "all cats are beautifull",
   155  	})
   156  	if err != nil {
   157  		t.Fatal(err)
   158  	}
   159  	authorizer := notification.NewAuthorizer(store)
   160  
   161  	t.Run("Background -> Unauthorized", func(t *testing.T) {
   162  		err := authorizer.Delete(context.Background(), "0")
   163  		if err == nil {
   164  			t.Error("should return unauthorized error")
   165  		}
   166  	})
   167  
   168  	t.Run("Different Normal user -> Unauthorized", func(t *testing.T) {
   169  		err := authorizer.Delete(auth.ContextWithID(normalCtx, "foo"), "0")
   170  		if err == nil {
   171  			t.Error("should return unauthorized error")
   172  		}
   173  	})
   174  
   175  	t.Run("Same Normal user -> Authorized", func(t *testing.T) {
   176  		err := authorizer.Delete(auth.ContextWithID(normalCtx, "1312"), "0")
   177  		if err != nil {
   178  			t.Error("should not return unauthorized error")
   179  		}
   180  		_, err = store.Create(context.Background(), &notification.NewNotification{
   181  			UserID:  "1312",
   182  			Message: "all cats are beautifull",
   183  		})
   184  		if err != nil {
   185  			t.Fatal(err)
   186  		}
   187  	})
   188  
   189  	t.Run("Different Moderator -> Unauthorized", func(t *testing.T) {
   190  		err := authorizer.Delete(auth.ContextWithID(moderatorCtx, "foo"), "0")
   191  		if err == nil {
   192  			t.Error("should return unauthorized error")
   193  		}
   194  	})
   195  
   196  	t.Run("Same Moderator -> Authorized", func(t *testing.T) {
   197  		err := authorizer.Delete(auth.ContextWithID(moderatorCtx, "1312"), "0")
   198  		if err != nil {
   199  			t.Error("should not return unauthorized error")
   200  		}
   201  		_, err = store.Create(context.Background(), &notification.NewNotification{
   202  			UserID:  "1312",
   203  			Message: "all cats are beautifull",
   204  		})
   205  		if err != nil {
   206  			t.Fatal(err)
   207  		}
   208  	})
   209  
   210  	t.Run("Admin -> Authorized", func(t *testing.T) {
   211  		err := authorizer.Delete(adminCtx, "0")
   212  		if err != nil {
   213  			t.Error("should not return unauthorized error")
   214  		}
   215  		_, err = store.Create(context.Background(), &notification.NewNotification{
   216  			UserID:  "1312",
   217  			Message: "all cats are beautifull",
   218  		})
   219  		if err != nil {
   220  			t.Fatal(err)
   221  		}
   222  	})
   223  
   224  	t.Run("Internal -> Authorized", func(t *testing.T) {
   225  		err := authorizer.Delete(internalCtx, "0")
   226  		if err != nil {
   227  			t.Error("should not return unauthorized error")
   228  		}
   229  	})
   230  }
   231  
   232  func TestAuthorizerFindByID(t *testing.T) {
   233  	store := notification.NewMemoryStore()
   234  	_, err := store.Create(context.Background(), &notification.NewNotification{
   235  		UserID:  "1312",
   236  		Message: "all cats are beautifull",
   237  	})
   238  	if err != nil {
   239  		t.Fatal(err)
   240  	}
   241  	authorizer := notification.NewAuthorizer(store)
   242  
   243  	t.Run("Background -> Unauthorized", func(t *testing.T) {
   244  		_, err := authorizer.FindByID(context.Background(), "0")
   245  		if err == nil {
   246  			t.Error("should return unauthorized error")
   247  		}
   248  	})
   249  
   250  	t.Run("Different Normal user -> Unauthorized", func(t *testing.T) {
   251  		_, err := authorizer.FindByID(auth.ContextWithID(normalCtx, "foo"), "0")
   252  		if err == nil {
   253  			t.Error("should return unauthorized error")
   254  		}
   255  	})
   256  
   257  	t.Run("Same Normal user -> Authorized", func(t *testing.T) {
   258  		_, err := authorizer.FindByID(auth.ContextWithID(normalCtx, "1312"), "0")
   259  		if err != nil {
   260  			t.Error("should not return unauthorized error")
   261  		}
   262  	})
   263  
   264  	t.Run("Different Moderator -> Unauthorized", func(t *testing.T) {
   265  		_, err := authorizer.FindByID(auth.ContextWithID(moderatorCtx, "foo"), "0")
   266  		if err == nil {
   267  			t.Error("should return unauthorized error")
   268  		}
   269  	})
   270  
   271  	t.Run("Same Moderator -> Authorized", func(t *testing.T) {
   272  		_, err := authorizer.FindByID(auth.ContextWithID(moderatorCtx, "1312"), "0")
   273  		if err != nil {
   274  			t.Error("should not return unauthorized error")
   275  		}
   276  	})
   277  
   278  	t.Run("Admin -> Authorized", func(t *testing.T) {
   279  		_, err := authorizer.FindByID(adminCtx, "0")
   280  		if err != nil {
   281  			t.Error("should not return unauthorized error")
   282  		}
   283  	})
   284  
   285  	t.Run("Internal -> Authorized", func(t *testing.T) {
   286  		_, err := authorizer.FindByID(internalCtx, "0")
   287  		if err != nil {
   288  			t.Error("should not return unauthorized error")
   289  		}
   290  	})
   291  }
   292  
   293  func TestAuthorizerFind(t *testing.T) {
   294  	store := notification.NewMemoryStore()
   295  	_, err := store.Create(context.Background(), &notification.NewNotification{
   296  		UserID:  "1312",
   297  		Message: "all cats are beautifull",
   298  	})
   299  	if err != nil {
   300  		t.Fatal(err)
   301  	}
   302  	_, err = store.Create(context.Background(), &notification.NewNotification{
   303  		UserID:  "0",
   304  		Message: "foo",
   305  	})
   306  	if err != nil {
   307  		t.Fatal(err)
   308  	}
   309  	authorizer := notification.NewAuthorizer(store)
   310  
   311  	t.Run("Background -> Unauthorized", func(t *testing.T) {
   312  		_, _, err := authorizer.Find(context.Background(), nil)
   313  		if err == nil {
   314  			t.Error("should return unauthorized error")
   315  		}
   316  	})
   317  
   318  	t.Run("Different Normal user -> No Result", func(t *testing.T) {
   319  		_, total, err := authorizer.Find(auth.ContextWithID(normalCtx, "foo"), nil)
   320  		if err != nil {
   321  			t.Error("should not return unauthorized error")
   322  		}
   323  		if total != 0 {
   324  			t.Errorf("wrong total: %d", total)
   325  		}
   326  	})
   327  
   328  	t.Run("Same Normal user -> Authorized", func(t *testing.T) {
   329  		_, total, err := authorizer.Find(auth.ContextWithID(normalCtx, "1312"), nil)
   330  		if err != nil {
   331  			t.Error("should not return unauthorized error")
   332  		}
   333  		if total != 1 {
   334  			t.Errorf("wrong total: %d", total)
   335  		}
   336  	})
   337  
   338  	t.Run("Different Moderator -> Unauthorized", func(t *testing.T) {
   339  		_, total, err := authorizer.Find(auth.ContextWithID(moderatorCtx, "foo"), nil)
   340  		if err != nil {
   341  			t.Error("should not return unauthorized error")
   342  		}
   343  		if total != 0 {
   344  			t.Errorf("wrong total: %d", total)
   345  		}
   346  	})
   347  
   348  	t.Run("Same Moderator -> Authorized", func(t *testing.T) {
   349  		_, total, err := authorizer.Find(auth.ContextWithID(moderatorCtx, "1312"), nil)
   350  		if err != nil {
   351  			t.Error("should not return unauthorized error")
   352  		}
   353  		if total != 1 {
   354  			t.Errorf("wrong total: %d", total)
   355  		}
   356  	})
   357  
   358  	t.Run("Admin -> Authorized", func(t *testing.T) {
   359  		_, total, err := authorizer.Find(adminCtx, nil)
   360  		if err != nil {
   361  			t.Error("should not return unauthorized error")
   362  		}
   363  		if total != 2 {
   364  			t.Errorf("wrong total: %d", total)
   365  		}
   366  	})
   367  
   368  	t.Run("Internal -> Authorized", func(t *testing.T) {
   369  		_, total, err := authorizer.Find(internalCtx, nil)
   370  		if err != nil {
   371  			t.Error("should not return unauthorized error")
   372  		}
   373  		if total != 2 {
   374  			t.Errorf("wrong total: %d", total)
   375  		}
   376  	})
   377  }
   378  
   379  func TestSettingsAuthorizerCreate(t *testing.T) {
   380  	store := notification.NewSettingsMemoryStore()
   381  	authorizer := notification.NewSettingsAuthorizer(store)
   382  
   383  	t.Run("Background context -> Unauthorized", func(t *testing.T) {
   384  		_, err := authorizer.Create(context.Background(), &notification.NewSettings{})
   385  		if err == nil {
   386  			t.Error("should return unauthorized error")
   387  		}
   388  	})
   389  
   390  	t.Run("Normal -> Unauthorized", func(t *testing.T) {
   391  		_, err := authorizer.Create(auth.ContextWithID(normalCtx, "1312"), &notification.NewSettings{UserID: "foo"})
   392  		if err == nil {
   393  			t.Error("should return unauthorized error")
   394  		}
   395  	})
   396  
   397  	t.Run("Normal (self)-> Authorized", func(t *testing.T) {
   398  		_, err := authorizer.Create(auth.ContextWithID(normalCtx, "1312"), &notification.NewSettings{UserID: "1312"})
   399  		if err != nil {
   400  			t.Error("should not return unauthorized error")
   401  		}
   402  	})
   403  
   404  	t.Run("Moderator -> Unauthorized", func(t *testing.T) {
   405  		_, err := authorizer.Create(auth.ContextWithID(moderatorCtx, "1312"), &notification.NewSettings{UserID: "foo"})
   406  		if err == nil {
   407  			t.Error("should return unauthorized error")
   408  		}
   409  	})
   410  
   411  	t.Run("Moderator (self)-> Authorized", func(t *testing.T) {
   412  		_, err := authorizer.Create(auth.ContextWithID(moderatorCtx, "1312"), &notification.NewSettings{UserID: "1312"})
   413  		if err != nil {
   414  			t.Error("should not return unauthorized error")
   415  		}
   416  	})
   417  
   418  	t.Run("Admin -> Authorized", func(t *testing.T) {
   419  		_, err := authorizer.Create(adminCtx, &notification.NewSettings{UserID: "foo"})
   420  		if err != nil {
   421  			t.Error("should not return unauthorized error")
   422  		}
   423  	})
   424  
   425  	t.Run("Internal -> Authorized", func(t *testing.T) {
   426  		_, err := authorizer.Create(internalCtx, &notification.NewSettings{UserID: "foo"})
   427  		if err != nil {
   428  			t.Error("should not return unauthorized error")
   429  		}
   430  	})
   431  }
   432  
   433  func TestSettingsAuthorizerUpdate(t *testing.T) {
   434  	store := notification.NewSettingsMemoryStore()
   435  	_, err := store.Create(context.Background(), &notification.NewSettings{
   436  		UserID: "1312",
   437  		Email:  false,
   438  	})
   439  	if err != nil {
   440  		t.Fatal(err)
   441  	}
   442  	authorizer := notification.NewSettingsAuthorizer(store)
   443  
   444  	t.Run("Background -> Unauthorized", func(t *testing.T) {
   445  		_, err := authorizer.Update(context.Background(), &notification.Settings{})
   446  		if err == nil {
   447  			t.Error("should return unauthorized error")
   448  		}
   449  	})
   450  
   451  	t.Run("Different Normal user -> Unauthorized", func(t *testing.T) {
   452  		_, err := authorizer.Update(auth.ContextWithID(normalCtx, "foo"), &notification.Settings{
   453  			UserID: "1312",
   454  			Email:  true,
   455  		})
   456  		if err == nil {
   457  			t.Error("should return unauthorized error")
   458  		}
   459  	})
   460  
   461  	t.Run("Same Normal user -> Authorized", func(t *testing.T) {
   462  		_, err := authorizer.Update(auth.ContextWithID(normalCtx, "1312"), &notification.Settings{
   463  			UserID: "1312",
   464  			Email:  true,
   465  		})
   466  		if err != nil {
   467  			t.Error("should not return unauthorized error")
   468  		}
   469  	})
   470  
   471  	t.Run("Different Moderator -> Unauthorized", func(t *testing.T) {
   472  		_, err := authorizer.Update(auth.ContextWithID(moderatorCtx, "foo"), &notification.Settings{
   473  			UserID: "1312",
   474  			Email:  true,
   475  		})
   476  		if err == nil {
   477  			t.Error("should return unauthorized error")
   478  		}
   479  	})
   480  
   481  	t.Run("Same Moderator -> Authorized", func(t *testing.T) {
   482  		_, err := authorizer.Update(auth.ContextWithID(moderatorCtx, "1312"), &notification.Settings{
   483  			UserID: "1312",
   484  			Email:  true,
   485  		})
   486  		if err != nil {
   487  			t.Errorf("should not return unauthorized error: %s", err)
   488  		}
   489  	})
   490  
   491  	t.Run("Admin -> Authorized", func(t *testing.T) {
   492  		_, err := authorizer.Update(adminCtx, &notification.Settings{UserID: "1312", Email: true})
   493  		if err != nil {
   494  			t.Error("should not return unauthorized error")
   495  		}
   496  	})
   497  
   498  	t.Run("Internal -> Authorized", func(t *testing.T) {
   499  		_, err := authorizer.Update(internalCtx, &notification.Settings{UserID: "1312", Email: true})
   500  		if err != nil {
   501  			t.Error("should not return unauthorized error")
   502  		}
   503  	})
   504  }
   505  
   506  func TestSettingsAuthorizerDelete(t *testing.T) {
   507  	store := notification.NewSettingsMemoryStore()
   508  	_, err := store.Create(context.Background(), &notification.NewSettings{
   509  		UserID: "1312",
   510  	})
   511  	if err != nil {
   512  		t.Fatal(err)
   513  	}
   514  	authorizer := notification.NewSettingsAuthorizer(store)
   515  
   516  	t.Run("Background -> Unauthorized", func(t *testing.T) {
   517  		err := authorizer.Delete(context.Background(), "1312")
   518  		if err == nil {
   519  			t.Error("should return unauthorized error")
   520  		}
   521  	})
   522  
   523  	t.Run("Different Normal user -> Unauthorized", func(t *testing.T) {
   524  		err := authorizer.Delete(auth.ContextWithID(normalCtx, "foo"), "1312")
   525  		if err == nil {
   526  			t.Error("should return unauthorized error")
   527  		}
   528  	})
   529  
   530  	t.Run("Same Normal user -> Authorized", func(t *testing.T) {
   531  		err := authorizer.Delete(auth.ContextWithID(normalCtx, "1312"), "1312")
   532  		if err != nil {
   533  			t.Errorf("should not return unauthorized error: %s", err)
   534  		}
   535  		_, err = store.Create(context.Background(), &notification.NewSettings{
   536  			UserID: "1312",
   537  		})
   538  		if err != nil {
   539  			t.Fatal(err)
   540  		}
   541  	})
   542  
   543  	t.Run("Different Moderator -> Unauthorized", func(t *testing.T) {
   544  		err := authorizer.Delete(auth.ContextWithID(moderatorCtx, "foo"), "1312")
   545  		if err == nil {
   546  			t.Error("should return unauthorized error")
   547  		}
   548  	})
   549  
   550  	t.Run("Same Moderator -> Authorized", func(t *testing.T) {
   551  		err := authorizer.Delete(auth.ContextWithID(moderatorCtx, "1312"), "1312")
   552  		if err != nil {
   553  			t.Error("should not return unauthorized error")
   554  		}
   555  		_, err = store.Create(context.Background(), &notification.NewSettings{
   556  			UserID: "1312",
   557  		})
   558  		if err != nil {
   559  			t.Fatal(err)
   560  		}
   561  	})
   562  
   563  	t.Run("Admin -> Authorized", func(t *testing.T) {
   564  		err := authorizer.Delete(adminCtx, "1312")
   565  		if err != nil {
   566  			t.Error("should not return unauthorized error")
   567  		}
   568  		_, err = store.Create(context.Background(), &notification.NewSettings{
   569  			UserID: "1312",
   570  		})
   571  		if err != nil {
   572  			t.Fatal(err)
   573  		}
   574  	})
   575  
   576  	t.Run("Internal -> Authorized", func(t *testing.T) {
   577  		err := authorizer.Delete(internalCtx, "1312")
   578  		if err != nil {
   579  			t.Error("should not return unauthorized error")
   580  		}
   581  	})
   582  }
   583  
   584  func TestSettingsAuthorizerFindByID(t *testing.T) {
   585  	store := notification.NewSettingsMemoryStore()
   586  	_, err := store.Create(context.Background(), &notification.NewSettings{
   587  		UserID: "1312",
   588  	})
   589  	if err != nil {
   590  		t.Fatal(err)
   591  	}
   592  	authorizer := notification.NewSettingsAuthorizer(store)
   593  
   594  	t.Run("Background -> Unauthorized", func(t *testing.T) {
   595  		_, err := authorizer.FindByID(context.Background(), "1312")
   596  		if err == nil {
   597  			t.Error("should return unauthorized error")
   598  		}
   599  	})
   600  
   601  	t.Run("Different Normal user -> Unauthorized", func(t *testing.T) {
   602  		_, err := authorizer.FindByID(auth.ContextWithID(normalCtx, "foo"), "1312")
   603  		if err == nil {
   604  			t.Error("should return unauthorized error")
   605  		}
   606  	})
   607  
   608  	t.Run("Same Normal user -> Authorized", func(t *testing.T) {
   609  		_, err := authorizer.FindByID(auth.ContextWithID(normalCtx, "1312"), "1312")
   610  		if err != nil {
   611  			t.Error("should not return unauthorized error")
   612  		}
   613  	})
   614  
   615  	t.Run("Different Moderator -> Unauthorized", func(t *testing.T) {
   616  		_, err := authorizer.FindByID(auth.ContextWithID(moderatorCtx, "foo"), "1312")
   617  		if err == nil {
   618  			t.Error("should return unauthorized error")
   619  		}
   620  	})
   621  
   622  	t.Run("Same Moderator -> Authorized", func(t *testing.T) {
   623  		_, err := authorizer.FindByID(auth.ContextWithID(moderatorCtx, "1312"), "1312")
   624  		if err != nil {
   625  			t.Error("should not return unauthorized error")
   626  		}
   627  	})
   628  
   629  	t.Run("Admin -> Authorized", func(t *testing.T) {
   630  		_, err := authorizer.FindByID(adminCtx, "1312")
   631  		if err != nil {
   632  			t.Error("should not return unauthorized error")
   633  		}
   634  	})
   635  
   636  	t.Run("Internal -> Authorized", func(t *testing.T) {
   637  		_, err := authorizer.FindByID(internalCtx, "1312")
   638  		if err != nil {
   639  			t.Error("should not return unauthorized error")
   640  		}
   641  	})
   642  }
   643  
   644  func TestSettingsAuthorizerFind(t *testing.T) {
   645  	store := notification.NewSettingsMemoryStore()
   646  	_, err := store.Create(context.Background(), &notification.NewSettings{
   647  		UserID: "1312",
   648  	})
   649  	if err != nil {
   650  		t.Fatal(err)
   651  	}
   652  	_, err = store.Create(context.Background(), &notification.NewSettings{
   653  		UserID: "0",
   654  	})
   655  	if err != nil {
   656  		t.Fatal(err)
   657  	}
   658  	authorizer := notification.NewSettingsAuthorizer(store)
   659  
   660  	t.Run("Background -> Unauthorized", func(t *testing.T) {
   661  		_, _, err := authorizer.Find(context.Background(), nil)
   662  		if err == nil {
   663  			t.Error("should return unauthorized error")
   664  		}
   665  	})
   666  
   667  	t.Run("Different Normal user -> No Result", func(t *testing.T) {
   668  		_, total, err := authorizer.Find(auth.ContextWithID(normalCtx, "foo"), nil)
   669  		if err != nil {
   670  			t.Error("should not return unauthorized error")
   671  		}
   672  		if total != 0 {
   673  			t.Errorf("wrong total: %d", total)
   674  		}
   675  	})
   676  
   677  	t.Run("Same Normal user -> Authorized", func(t *testing.T) {
   678  		_, total, err := authorizer.Find(auth.ContextWithID(normalCtx, "1312"), nil)
   679  		if err != nil {
   680  			t.Error("should not return unauthorized error")
   681  		}
   682  		if total != 1 {
   683  			t.Errorf("wrong total: %d", total)
   684  		}
   685  	})
   686  
   687  	t.Run("Different Moderator -> Unauthorized", func(t *testing.T) {
   688  		_, total, err := authorizer.Find(auth.ContextWithID(moderatorCtx, "foo"), nil)
   689  		if err != nil {
   690  			t.Error("should not return unauthorized error")
   691  		}
   692  		if total != 0 {
   693  			t.Errorf("wrong total: %d", total)
   694  		}
   695  	})
   696  
   697  	t.Run("Same Moderator -> Authorized", func(t *testing.T) {
   698  		_, total, err := authorizer.Find(auth.ContextWithID(moderatorCtx, "1312"), nil)
   699  		if err != nil {
   700  			t.Error("should not return unauthorized error")
   701  		}
   702  		if total != 1 {
   703  			t.Errorf("wrong total: %d", total)
   704  		}
   705  	})
   706  
   707  	t.Run("Admin -> Authorized", func(t *testing.T) {
   708  		_, total, err := authorizer.Find(adminCtx, nil)
   709  		if err != nil {
   710  			t.Error("should not return unauthorized error")
   711  		}
   712  		if total != 2 {
   713  			t.Errorf("wrong total: %d", total)
   714  		}
   715  	})
   716  
   717  	t.Run("Internal -> Authorized", func(t *testing.T) {
   718  		_, total, err := authorizer.Find(internalCtx, nil)
   719  		if err != nil {
   720  			t.Error("should not return unauthorized error")
   721  		}
   722  		if total != 2 {
   723  			t.Errorf("wrong total: %d", total)
   724  		}
   725  	})
   726  }