[MM-16512] Migrate Token.Save to Sync by default (#11353)

* [MM-16512] Migrate Token.Save to Sync by default

* refactor: formatting

* test: add checks for errors while saving tokens

* test: fixup
Этот коммит содержится в:
krjn
2019-06-26 10:36:41 +00:00
коммит произвёл Sudheer
родитель 2327ab8854
Коммит 72d482de0e
11 изменённых файлов: 42 добавлений и 44 удалений

Просмотреть файл

@@ -1436,7 +1436,7 @@ func TestAddTeamMember(t *testing.T) {
app.TOKEN_TYPE_TEAM_INVITATION, app.TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": team.Id}), model.MapToJson(map[string]string{"teamId": team.Id}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
tm, resp = Client.AddTeamMemberFromInvite(token.Token, "") tm, resp = Client.AddTeamMemberFromInvite(token.Token, "")
CheckNoError(t, resp) CheckNoError(t, resp)
@@ -1466,7 +1466,7 @@ func TestAddTeamMember(t *testing.T) {
// expired token of more than 50 hours // expired token of more than 50 hours
token = model.NewToken(app.TOKEN_TYPE_TEAM_INVITATION, "") token = model.NewToken(app.TOKEN_TYPE_TEAM_INVITATION, "")
token.CreateAt = model.GetMillis() - 1000*60*60*50 token.CreateAt = model.GetMillis() - 1000*60*60*50
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
_, resp = Client.AddTeamMemberFromInvite(token.Token, "") _, resp = Client.AddTeamMemberFromInvite(token.Token, "")
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
@@ -1478,7 +1478,7 @@ func TestAddTeamMember(t *testing.T) {
app.TOKEN_TYPE_TEAM_INVITATION, app.TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": testId}), model.MapToJson(map[string]string{"teamId": testId}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
_, resp = Client.AddTeamMemberFromInvite(token.Token, "") _, resp = Client.AddTeamMemberFromInvite(token.Token, "")
CheckNotFoundStatus(t, resp) CheckNotFoundStatus(t, resp)
@@ -1519,7 +1519,7 @@ func TestAddTeamMember(t *testing.T) {
app.TOKEN_TYPE_TEAM_INVITATION, app.TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": team.Id}), model.MapToJson(map[string]string{"teamId": team.Id}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
tm, resp = Client.AddTeamMemberFromInvite(token.Token, "") tm, resp = Client.AddTeamMemberFromInvite(token.Token, "")
require.Equal(t, "app.team.invite_token.group_constrained.error", resp.Error.Id) require.Equal(t, "app.team.invite_token.group_constrained.error", resp.Error.Id)

Просмотреть файл

@@ -93,7 +93,7 @@ func TestCreateUserWithToken(t *testing.T) {
app.TOKEN_TYPE_TEAM_INVITATION, app.TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
ruser, resp := th.Client.CreateUserWithToken(&user, token.Token) ruser, resp := th.Client.CreateUserWithToken(&user, token.Token)
CheckNoError(t, resp) CheckNoError(t, resp)
@@ -124,7 +124,7 @@ func TestCreateUserWithToken(t *testing.T) {
app.TOKEN_TYPE_TEAM_INVITATION, app.TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
defer th.App.DeleteToken(token) defer th.App.DeleteToken(token)
_, resp := th.Client.CreateUserWithToken(&user, "") _, resp := th.Client.CreateUserWithToken(&user, "")
@@ -141,7 +141,7 @@ func TestCreateUserWithToken(t *testing.T) {
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}),
) )
token.CreateAt = past49Hours token.CreateAt = past49Hours
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
defer th.App.DeleteToken(token) defer th.App.DeleteToken(token)
_, resp := th.Client.CreateUserWithToken(&user, token.Token) _, resp := th.Client.CreateUserWithToken(&user, token.Token)
@@ -170,7 +170,7 @@ func TestCreateUserWithToken(t *testing.T) {
app.TOKEN_TYPE_TEAM_INVITATION, app.TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
defer th.App.DeleteToken(token) defer th.App.DeleteToken(token)
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableUserCreation = false }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableUserCreation = false })
@@ -188,7 +188,7 @@ func TestCreateUserWithToken(t *testing.T) {
app.TOKEN_TYPE_TEAM_INVITATION, app.TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
enableOpenServer := th.App.Config().TeamSettings.EnableOpenServer enableOpenServer := th.App.Config().TeamSettings.EnableOpenServer
defer func() { defer func() {

Просмотреть файл

@@ -335,8 +335,8 @@ func (a *App) SendInviteEmails(team *model.Team, senderName string, senderUserId
props["name"] = team.Name props["name"] = team.Name
data := model.MapToJson(props) data := model.MapToJson(props)
if result := <-a.Srv.Store.Token().Save(token); result.Err != nil { if err := a.Srv.Store.Token().Save(token); err != nil {
mlog.Error(fmt.Sprintf("Failed to send invite email successfully err=%v", result.Err)) mlog.Error(fmt.Sprintf("Failed to send invite email successfully err=%v", err))
continue continue
} }
bodyPage.Props["Link"] = fmt.Sprintf("%s/signup_user_complete/?d=%s&t=%s", siteURL, url.QueryEscape(data), url.QueryEscape(token.Token)) bodyPage.Props["Link"] = fmt.Sprintf("%s/signup_user_complete/?d=%s&t=%s", siteURL, url.QueryEscape(data), url.QueryEscape(token.Token))

Просмотреть файл

@@ -593,8 +593,8 @@ func (a *App) CompleteSwitchWithOAuth(service string, userData io.Reader, email
func (a *App) CreateOAuthStateToken(extra string) (*model.Token, *model.AppError) { func (a *App) CreateOAuthStateToken(extra string) (*model.Token, *model.AppError) {
token := model.NewToken(model.TOKEN_TYPE_OAUTH, extra) token := model.NewToken(model.TOKEN_TYPE_OAUTH, extra)
if result := <-a.Srv.Store.Token().Save(token); result.Err != nil { if err := a.Srv.Store.Token().Save(token); err != nil {
return nil, result.Err return nil, err
} }
return token, nil return token, nil

Просмотреть файл

@@ -233,8 +233,7 @@ func TestAuthorizeOAuthUser(t *testing.T) {
defer th.TearDown() defer th.TearDown()
token := model.NewToken("invalid", "") token := model.NewToken("invalid", "")
result := <-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
require.Nil(t, result.Err)
state := makeState(token) state := makeState(token)

Просмотреть файл

@@ -205,7 +205,7 @@ func TestAddUserToTeamByToken(t *testing.T) {
TOKEN_TYPE_VERIFY_EMAIL, TOKEN_TYPE_VERIFY_EMAIL,
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
defer th.App.DeleteToken(token) defer th.App.DeleteToken(token)
if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil { if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil {
t.Fatal("Should fail on bad token type") t.Fatal("Should fail on bad token type")
@@ -218,7 +218,7 @@ func TestAddUserToTeamByToken(t *testing.T) {
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}),
) )
token.CreateAt = model.GetMillis() - TEAM_INVITATION_EXPIRY_TIME - 1 token.CreateAt = model.GetMillis() - TEAM_INVITATION_EXPIRY_TIME - 1
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
defer th.App.DeleteToken(token) defer th.App.DeleteToken(token)
if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil { if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil {
t.Fatal("Should fail on expired token") t.Fatal("Should fail on expired token")
@@ -230,7 +230,7 @@ func TestAddUserToTeamByToken(t *testing.T) {
TOKEN_TYPE_TEAM_INVITATION, TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": model.NewId()}), model.MapToJson(map[string]string{"teamId": model.NewId()}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
defer th.App.DeleteToken(token) defer th.App.DeleteToken(token)
if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil { if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil {
t.Fatal("Should fail on bad team id") t.Fatal("Should fail on bad team id")
@@ -242,7 +242,7 @@ func TestAddUserToTeamByToken(t *testing.T) {
TOKEN_TYPE_TEAM_INVITATION, TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
defer th.App.DeleteToken(token) defer th.App.DeleteToken(token)
if _, err := th.App.AddUserToTeamByToken(model.NewId(), token.Token); err == nil { if _, err := th.App.AddUserToTeamByToken(model.NewId(), token.Token); err == nil {
t.Fatal("Should fail on bad user id") t.Fatal("Should fail on bad user id")
@@ -254,7 +254,7 @@ func TestAddUserToTeamByToken(t *testing.T) {
TOKEN_TYPE_TEAM_INVITATION, TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err != nil { if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err != nil {
t.Log(err) t.Log(err)
t.Fatal("Should add user to the team") t.Fatal("Should add user to the team")
@@ -274,7 +274,7 @@ func TestAddUserToTeamByToken(t *testing.T) {
TOKEN_TYPE_TEAM_INVITATION, TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil { if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil {
t.Fatal("Should return an error when trying to join a group-constrained team.") t.Fatal("Should return an error when trying to join a group-constrained team.")
} else { } else {
@@ -303,7 +303,7 @@ func TestAddUserToTeamByToken(t *testing.T) {
TOKEN_TYPE_TEAM_INVITATION, TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil || err.Where != "JoinUserToTeam" { if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil || err.Where != "JoinUserToTeam" {
t.Log(err) t.Log(err)

Просмотреть файл

@@ -1349,8 +1349,8 @@ func (a *App) CreatePasswordRecoveryToken(userId, email string) (*model.Token, *
token := model.NewToken(TOKEN_TYPE_PASSWORD_RECOVERY, string(jsonData)) token := model.NewToken(TOKEN_TYPE_PASSWORD_RECOVERY, string(jsonData))
if result := <-a.Srv.Store.Token().Save(token); result.Err != nil { if err := a.Srv.Store.Token().Save(token); err != nil {
return nil, result.Err return nil, err
} }
return token, nil return token, nil
@@ -1615,8 +1615,8 @@ func (a *App) CreateVerifyEmailToken(userId string, newEmail string) (*model.Tok
token := model.NewToken(TOKEN_TYPE_VERIFY_EMAIL, string(jsonData)) token := model.NewToken(TOKEN_TYPE_VERIFY_EMAIL, string(jsonData))
if result := <-a.Srv.Store.Token().Save(token); result.Err != nil { if err := a.Srv.Store.Token().Save(token); err != nil {
return nil, result.Err return nil, err
} }
return token, nil return token, nil

Просмотреть файл

@@ -571,7 +571,7 @@ func TestCreateUserWithToken(t *testing.T) {
TOKEN_TYPE_VERIFY_EMAIL, TOKEN_TYPE_VERIFY_EMAIL,
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
defer th.App.DeleteToken(token) defer th.App.DeleteToken(token)
if _, err := th.App.CreateUserWithToken(&user, token.Token); err == nil { if _, err := th.App.CreateUserWithToken(&user, token.Token); err == nil {
t.Fatal("Should fail on bad token type") t.Fatal("Should fail on bad token type")
@@ -584,7 +584,7 @@ func TestCreateUserWithToken(t *testing.T) {
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": user.Email}),
) )
token.CreateAt = model.GetMillis() - TEAM_INVITATION_EXPIRY_TIME - 1 token.CreateAt = model.GetMillis() - TEAM_INVITATION_EXPIRY_TIME - 1
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
defer th.App.DeleteToken(token) defer th.App.DeleteToken(token)
if _, err := th.App.CreateUserWithToken(&user, token.Token); err == nil { if _, err := th.App.CreateUserWithToken(&user, token.Token); err == nil {
t.Fatal("Should fail on expired token") t.Fatal("Should fail on expired token")
@@ -596,7 +596,7 @@ func TestCreateUserWithToken(t *testing.T) {
TOKEN_TYPE_TEAM_INVITATION, TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": model.NewId(), "email": user.Email}), model.MapToJson(map[string]string{"teamId": model.NewId(), "email": user.Email}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
defer th.App.DeleteToken(token) defer th.App.DeleteToken(token)
if _, err := th.App.CreateUserWithToken(&user, token.Token); err == nil { if _, err := th.App.CreateUserWithToken(&user, token.Token); err == nil {
t.Fatal("Should fail on bad team id") t.Fatal("Should fail on bad team id")
@@ -609,7 +609,7 @@ func TestCreateUserWithToken(t *testing.T) {
TOKEN_TYPE_TEAM_INVITATION, TOKEN_TYPE_TEAM_INVITATION,
model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": invitationEmail}), model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id, "email": invitationEmail}),
) )
<-th.App.Srv.Store.Token().Save(token) require.Nil(t, th.App.Srv.Store.Token().Save(token))
newUser, err := th.App.CreateUserWithToken(&user, token.Token) newUser, err := th.App.CreateUserWithToken(&user, token.Token)
if err != nil { if err != nil {
t.Log(err) t.Log(err)

Просмотреть файл

@@ -32,16 +32,15 @@ func NewSqlTokenStore(sqlStore SqlStore) store.TokenStore {
func (s SqlTokenStore) CreateIndexesIfNotExists() { func (s SqlTokenStore) CreateIndexesIfNotExists() {
} }
func (s SqlTokenStore) Save(token *model.Token) store.StoreChannel { func (s SqlTokenStore) Save(token *model.Token) *model.AppError {
return store.Do(func(result *store.StoreResult) { if err := token.IsValid(); err != nil {
if result.Err = token.IsValid(); result.Err != nil { return err
return }
}
if err := s.GetMaster().Insert(token); err != nil { if err := s.GetMaster().Insert(token); err != nil {
result.Err = model.NewAppError("SqlTokenStore.Save", "store.sql_recover.save.app_error", nil, "", http.StatusInternalServerError) return model.NewAppError("SqlTokenStore.Save", "store.sql_recover.save.app_error", nil, "", http.StatusInternalServerError)
} }
}) return nil
} }
func (s SqlTokenStore) Delete(token string) store.StoreChannel { func (s SqlTokenStore) Delete(token string) store.StoreChannel {

Просмотреть файл

@@ -449,7 +449,7 @@ type LicenseStore interface {
} }
type TokenStore interface { type TokenStore interface {
Save(recovery *model.Token) StoreChannel Save(recovery *model.Token) *model.AppError
Delete(token string) StoreChannel Delete(token string) StoreChannel
GetByToken(token string) (*model.Token, *model.AppError) GetByToken(token string) (*model.Token, *model.AppError)
Cleanup() Cleanup()

Просмотреть файл

@@ -76,15 +76,15 @@ func (_m *TokenStore) RemoveAllTokensByType(tokenType string) store.StoreChannel
} }
// Save provides a mock function with given fields: recovery // Save provides a mock function with given fields: recovery
func (_m *TokenStore) Save(recovery *model.Token) store.StoreChannel { func (_m *TokenStore) Save(recovery *model.Token) *model.AppError {
ret := _m.Called(recovery) ret := _m.Called(recovery)
var r0 store.StoreChannel var r0 *model.AppError
if rf, ok := ret.Get(0).(func(*model.Token) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(*model.Token) *model.AppError); ok {
r0 = rf(recovery) r0 = rf(recovery)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AppError)
} }
} }