MCPcopy Index your code
hub / github.com/dnote/dnote / TestUpdateEmail

Function TestUpdateEmail

pkg/server/controllers/users_test.go:931–990  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

929}
930
931func TestUpdateEmail(t *testing.T) {
932 t.Run("success", func(t *testing.T) {
933 db := testutils.InitMemoryDB(t)
934
935 // Setup
936 a := app.NewTest()
937 a.Clock = clock.NewMock()
938 a.DB = db
939 server := MustNewServer(t, &a)
940 defer server.Close()
941
942 u := testutils.SetupUserData(db, "alice@example.com", "pass1234")
943
944 // Execute
945 dat := url.Values{}
946 dat.Set("email", "alice-new@example.com")
947 dat.Set("password", "pass1234")
948 req := testutils.MakeFormReq(server.URL, "PATCH", "/account/profile", dat)
949
950 res := testutils.HTTPAuthDo(t, db, req, u)
951
952 // Test
953 assert.StatusCodeEquals(t, res, http.StatusFound, "Status code mismatch")
954
955 var user database.User
956 testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding user")
957 testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding account")
958
959 assert.Equal(t, user.Email.String, "alice-new@example.com", "email mismatch")
960 })
961
962 t.Run("password mismatch", func(t *testing.T) {
963 db := testutils.InitMemoryDB(t)
964
965 // Setup
966 a := app.NewTest()
967 a.Clock = clock.NewMock()
968 a.DB = db
969 server := MustNewServer(t, &a)
970 defer server.Close()
971
972 u := testutils.SetupUserData(db, "alice@example.com", "pass1234")
973
974 // Execute
975 dat := url.Values{}
976 dat.Set("email", "alice-new@example.com")
977 dat.Set("password", "wrongpassword")
978 req := testutils.MakeFormReq(server.URL, "PATCH", "/account/profile", dat)
979
980 res := testutils.HTTPAuthDo(t, db, req, u)
981
982 // Test
983 assert.StatusCodeEquals(t, res, http.StatusUnauthorized, "Status code mismsatch")
984
985 var user database.User
986 testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding user")
987
988 assert.Equal(t, user.Email.String, "alice@example.com", "email mismatch")

Callers

nothing calls this directly

Calls 11

InitMemoryDBFunction · 0.92
NewTestFunction · 0.92
NewMockFunction · 0.92
SetupUserDataFunction · 0.92
MakeFormReqFunction · 0.92
HTTPAuthDoFunction · 0.92
StatusCodeEqualsFunction · 0.92
MustExecFunction · 0.92
EqualFunction · 0.92
MustNewServerFunction · 0.85
CloseMethod · 0.65

Tested by

no test coverage detected