2017-01-27 13:35:58 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
"github.com/fleetdm/fleet/server/fleet"
|
2017-01-27 13:35:58 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testChangeEmail(t *testing.T, ds fleet.Datastore) {
|
2017-01-27 13:35:58 +00:00
|
|
|
if ds.Name() == "inmem" {
|
|
|
|
t.Skip("inmem is being deprecated, test skipped")
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
user := &fleet.User{
|
2017-01-27 13:35:58 +00:00
|
|
|
Password: []byte("foobar"),
|
|
|
|
Email: "bob@bob.com",
|
|
|
|
}
|
|
|
|
user, err := ds.NewUser(user)
|
|
|
|
require.Nil(t, err)
|
|
|
|
err = ds.PendingEmailChange(user.ID, "xxxx@yyy.com", "abcd12345")
|
|
|
|
require.Nil(t, err)
|
2017-02-16 13:07:20 +00:00
|
|
|
newMail, err := ds.ConfirmPendingEmailChange(user.ID, "abcd12345")
|
2017-01-27 13:35:58 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, "xxxx@yyy.com", newMail)
|
|
|
|
user, err = ds.UserByID(user.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, "xxxx@yyy.com", user.Email)
|
|
|
|
// this should fail because it doesn't exist
|
2017-02-16 13:07:20 +00:00
|
|
|
newMail, err = ds.ConfirmPendingEmailChange(user.ID, "abcd12345")
|
2017-01-27 13:35:58 +00:00
|
|
|
assert.NotNil(t, err)
|
2017-02-16 13:07:20 +00:00
|
|
|
|
|
|
|
// test that wrong user can't confirm e-mail change
|
|
|
|
err = ds.PendingEmailChange(user.ID, "other@bob.com", "uniquetoken")
|
|
|
|
require.Nil(t, err)
|
2021-06-06 22:07:29 +00:00
|
|
|
otheruser, err := ds.NewUser(&fleet.User{
|
2017-02-16 13:07:20 +00:00
|
|
|
Password: []byte("supersecret"),
|
|
|
|
Email: "other@bobcom",
|
|
|
|
})
|
|
|
|
require.Nil(t, err)
|
|
|
|
_, err = ds.ConfirmPendingEmailChange(otheruser.ID, "uniquetoken")
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
2017-01-27 13:35:58 +00:00
|
|
|
}
|