eintopf.info@v0.13.16/service/user/user_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 user_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"eintopf.info/service/auth"
    23  	"eintopf.info/service/user"
    24  )
    25  
    26  func TestUserServiceCreate(t *testing.T) {
    27  	ctx := context.Background()
    28  
    29  	store := user.NewMemoryStore()
    30  	userService := user.NewService(store)
    31  
    32  	existingUser, err := store.Create(ctx, &user.NewUser{Email: "existing", Nickname: "existing"})
    33  	if err != nil {
    34  		t.Fatalf("userService.Create() failed: %s", err)
    35  	}
    36  
    37  	// Check for empty email
    38  	_, err = userService.Create(ctx, &user.NewUser{Email: "", Nickname: "foo", Password: "foo"})
    39  	if err == nil || err.Error() != "empty email" {
    40  		t.Errorf("userService.UseInvite should fail with empty email: %s", err)
    41  	}
    42  
    43  	// Check for empty nickname
    44  	_, err = userService.Create(ctx, &user.NewUser{Email: "foo", Nickname: "", Password: "foo"})
    45  	if err == nil || err.Error() != "empty nickname" {
    46  		t.Errorf("userService.UseInvite should fail with empty nickname: %s", err)
    47  	}
    48  
    49  	// Check for empty password
    50  	_, err = userService.Create(ctx, &user.NewUser{Email: "foo", Nickname: "foo", Password: ""})
    51  	if err == nil || err.Error() != "empty password" {
    52  		t.Errorf("userService.UseInvite should fail with empty password: %s", err)
    53  	}
    54  
    55  	// Check for existing email
    56  	_, err = userService.Create(ctx, &user.NewUser{Email: existingUser.Email, Nickname: "blub", Password: "123"})
    57  	if err == nil || err.Error() != "email already exists" {
    58  		t.Errorf("userService.UseInvite should fail with existing email: %s", err)
    59  	}
    60  
    61  	// Check for existing nickname
    62  	_, err = userService.Create(ctx, &user.NewUser{Email: "foo", Nickname: existingUser.Nickname, Password: "123"})
    63  	if err == nil || err.Error() != "nickname already exists" {
    64  		t.Errorf("userService.UseInvite should fail with existing nickname: %s", err)
    65  	}
    66  
    67  	_, err = userService.Create(ctx, &user.NewUser{Email: "foo", Nickname: "foo", Password: "123"})
    68  	if err != nil {
    69  		t.Errorf("userService.Create failed: %s", err)
    70  	}
    71  }
    72  
    73  func TestUserServicePassword(t *testing.T) {
    74  	ctx := context.Background()
    75  
    76  	store := user.NewMemoryStore()
    77  	userService := user.NewService(store)
    78  
    79  	// It creates a hashed password
    80  	u, err := userService.Create(ctx, &user.NewUser{Email: "foo", Nickname: "foo", Password: "123"})
    81  	if err != nil {
    82  		t.Errorf("userService.Create failed: %s", err.Error())
    83  	}
    84  	if u.Password == "123" {
    85  		t.Errorf("password should be hashed: got %s", u.Password)
    86  	}
    87  
    88  	// Validate uses hashed password
    89  	id, err := userService.Validate(ctx, "foo", "123")
    90  	if err != nil {
    91  		t.Errorf("userService.Validate failed: %s", err.Error())
    92  	}
    93  	if id == "" {
    94  		t.Errorf("user is invalid")
    95  	}
    96  }
    97  
    98  func TestUpdatePassword(t *testing.T) {
    99  	store := user.NewMemoryStore()
   100  	service := user.NewService(user.NewAuthorizer(store))
   101  
   102  	internalCtx := auth.ContextWithRole(context.Background(), auth.RoleInternal)
   103  	u, err := service.Create(internalCtx, &user.NewUser{
   104  		Email:    "email",
   105  		Nickname: "nickname",
   106  		Password: "foo",
   107  	})
   108  	if err != nil {
   109  		t.Fatalf("service.Create failed: %s", err)
   110  	}
   111  	id, err := service.Validate(internalCtx, "email", "foo")
   112  	if err != nil {
   113  		t.Errorf("service.Validate: %s", err)
   114  	}
   115  	if id == "" {
   116  		t.Error("password should be 'foo'")
   117  	}
   118  
   119  	ctx := auth.ContextWithID(auth.ContextWithRole(context.Background(), auth.RoleNormal), u.ID)
   120  	u = &user.User{ID: u.ID, Email: u.Email, Nickname: u.Nickname, Password: "bar"}
   121  	u, err = service.Update(ctx, u)
   122  	if err != nil {
   123  		t.Fatalf("service.Update failed: %s", err)
   124  	}
   125  	id, err = service.Validate(context.Background(), "email", "bar")
   126  	if err != nil {
   127  		t.Errorf("service.Validate: %s", err)
   128  	}
   129  	if id == "" {
   130  		t.Error("password should be 'bar' after updating")
   131  	}
   132  }
   133  
   134  func TestUpdatePasswordDoesntReset(t *testing.T) {
   135  	store := user.NewMemoryStore()
   136  	service := user.NewService(user.NewAuthorizer(store))
   137  
   138  	internalCtx := auth.ContextWithRole(context.Background(), auth.RoleInternal)
   139  	u, err := service.Create(internalCtx, &user.NewUser{
   140  		Email:    "email",
   141  		Nickname: "nickname",
   142  		Password: "foo",
   143  	})
   144  	if err != nil {
   145  		t.Fatalf("service.Create failed: %s", err)
   146  	}
   147  	id, err := service.Validate(context.Background(), "email", "foo")
   148  	if err != nil {
   149  		t.Errorf("service.Validate: %s", err)
   150  	}
   151  	if id == "" {
   152  		t.Error("password should be 'foo'")
   153  	}
   154  
   155  	ctx := auth.ContextWithID(auth.ContextWithRole(context.Background(), auth.RoleNormal), u.ID)
   156  	_, err = service.Update(ctx, &user.User{
   157  		ID:       u.ID,
   158  		Email:    u.Email,
   159  		Nickname: u.Nickname,
   160  		Password: "",
   161  	})
   162  	if err != nil {
   163  		t.Fatalf("service.Update failed: %s", err)
   164  	}
   165  	id, err = service.Validate(context.Background(), "email", "foo")
   166  	if err != nil {
   167  		t.Errorf("service.Validate: %s", err)
   168  	}
   169  	if id == "" {
   170  		t.Error("password should be 'foo' after updating")
   171  	}
   172  }