mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 09:18:59 +00:00
4f4185372d
This is just to pass down the context to the datastore layer, it doesn't use it just yet - this will be in a follow-up PR.
50 lines
1.5 KiB
Go
50 lines
1.5 KiB
Go
package mysql
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestChangeEmail(t *testing.T) {
|
|
ds := CreateMySQLDS(t)
|
|
defer ds.Close()
|
|
|
|
user := &fleet.User{
|
|
Password: []byte("foobar"),
|
|
Email: "bob@bob.com",
|
|
GlobalRole: ptr.String(fleet.RoleObserver),
|
|
}
|
|
user, err := ds.NewUser(context.Background(), user)
|
|
require.Nil(t, err)
|
|
err = ds.PendingEmailChange(context.Background(), user.ID, "xxxx@yyy.com", "abcd12345")
|
|
require.Nil(t, err)
|
|
newMail, err := ds.ConfirmPendingEmailChange(context.Background(), user.ID, "abcd12345")
|
|
require.Nil(t, err)
|
|
assert.Equal(t, "xxxx@yyy.com", newMail)
|
|
user, err = ds.UserByID(context.Background(), user.ID)
|
|
require.Nil(t, err)
|
|
assert.Equal(t, "xxxx@yyy.com", user.Email)
|
|
// this should fail because it doesn't exist
|
|
_, err = ds.ConfirmPendingEmailChange(context.Background(), user.ID, "abcd12345")
|
|
assert.NotNil(t, err)
|
|
|
|
// test that wrong user can't confirm e-mail change
|
|
err = ds.PendingEmailChange(context.Background(), user.ID, "other@bob.com", "uniquetoken")
|
|
require.Nil(t, err)
|
|
otheruser, err := ds.NewUser(context.Background(), &fleet.User{
|
|
Password: []byte("supersecret"),
|
|
Email: "other@bobcom",
|
|
GlobalRole: ptr.String(fleet.RoleObserver),
|
|
})
|
|
require.Nil(t, err)
|
|
_, err = ds.ConfirmPendingEmailChange(context.Background(), otheruser.ID, "uniquetoken")
|
|
assert.NotNil(t, err)
|
|
|
|
}
|