MCPcopy
hub / github.com/benbjohnson/wtf / TestUserService_UpdateUser

Function TestUserService_UpdateUser

sqlite/user_test.go:64–113  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

62}
63
64func TestUserService_UpdateUser(t *testing.T) {
65 // Ensure user name & email can be updated by current user.
66 t.Run("OK", func(t *testing.T) {
67 db := MustOpenDB(t)
68 defer MustCloseDB(t, db)
69 s := sqlite.NewUserService(db)
70 user0, ctx0 := MustCreateUser(t, context.Background(), db, &wtf.User{
71 Name: "susy",
72 Email: "susy@gmail.com",
73 })
74
75 // Update user.
76 newName, newEmail := "jill", "jill@gmail.com"
77 uu, err := s.UpdateUser(ctx0, user0.ID, wtf.UserUpdate{
78 Name: &newName,
79 Email: &newEmail,
80 })
81 if err != nil {
82 t.Fatal(err)
83 } else if got, want := uu.Name, "jill"; got != want {
84 t.Fatalf("Name=%v, want %v", got, want)
85 } else if got, want := uu.Email, "jill@gmail.com"; got != want {
86 t.Fatalf("Email=%v, want %v", got, want)
87 }
88
89 // Fetch user from database & compare.
90 if other, err := s.FindUserByID(context.Background(), 1); err != nil {
91 t.Fatal(err)
92 } else if !reflect.DeepEqual(uu, other) {
93 t.Fatalf("mismatch: %#v != %#v", uu, other)
94 }
95 })
96
97 // Ensure updating a user is restricted only to the current user.
98 t.Run("ErrUnauthorized", func(t *testing.T) {
99 db := MustOpenDB(t)
100 defer MustCloseDB(t, db)
101 s := sqlite.NewUserService(db)
102 user0, _ := MustCreateUser(t, context.Background(), db, &wtf.User{Name: "NAME0"})
103 _, ctx1 := MustCreateUser(t, context.Background(), db, &wtf.User{Name: "NAME1"})
104
105 // Update user as another user.
106 newName := "NEWNAME"
107 if _, err := s.UpdateUser(ctx1, user0.ID, wtf.UserUpdate{Name: &newName}); err == nil {
108 t.Fatal("expected error")
109 } else if wtf.ErrorCode(err) != wtf.EUNAUTHORIZED || wtf.ErrorMessage(err) != `You are not allowed to update this user.` {
110 t.Fatalf("unexpected error: %#v", err)
111 }
112 })
113}
114
115func TestUserService_DeleteUser(t *testing.T) {
116 // Ensure user can delete self.

Callers

nothing calls this directly

Calls 9

UpdateUserMethod · 0.95
FindUserByIDMethod · 0.95
NewUserServiceFunction · 0.92
ErrorCodeFunction · 0.92
ErrorMessageFunction · 0.92
MustOpenDBFunction · 0.85
MustCloseDBFunction · 0.85
MustCreateUserFunction · 0.70
RunMethod · 0.45

Tested by

no test coverage detected