Migrate OAuth store to sync by default (#11319)

* Migrate OAuth store to sync by default

* Removing unnecesary return oldValue for updateApp in Oauth Store
Этот коммит содержится в:
Jesús Espino
2019-06-21 18:21:18 +02:00
коммит произвёл GitHub
родитель e3c8c1251f
Коммит e3504398c7
8 изменённых файлов: 569 добавлений и 592 удалений

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

@@ -976,7 +976,8 @@ func TestOAuthAccessToken(t *testing.T) {
} }
authData := &model.AuthData{ClientId: oauthApp.Id, RedirectUri: oauthApp.CallbackUrls[0], UserId: th.BasicUser.Id, Code: model.NewId(), ExpiresIn: -1} authData := &model.AuthData{ClientId: oauthApp.Id, RedirectUri: oauthApp.CallbackUrls[0], UserId: th.BasicUser.Id, Code: model.NewId(), ExpiresIn: -1}
<-th.App.Srv.Store.OAuth().SaveAuthData(authData) _, err := th.App.Srv.Store.OAuth().SaveAuthData(authData)
require.Nil(t, err)
data.Set("grant_type", model.ACCESS_TOKEN_GRANT_TYPE) data.Set("grant_type", model.ACCESS_TOKEN_GRANT_TYPE)
data.Set("client_id", oauthApp.Id) data.Set("client_id", oauthApp.Id)

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

@@ -34,24 +34,14 @@ func (a *App) CreateOAuthApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppEr
app.ClientSecret = model.NewId() app.ClientSecret = model.NewId()
result := <-a.Srv.Store.OAuth().SaveApp(app) return a.Srv.Store.OAuth().SaveApp(app)
if result.Err != nil {
return nil, result.Err
}
return result.Data.(*model.OAuthApp), nil
} }
func (a *App) GetOAuthApp(appId string) (*model.OAuthApp, *model.AppError) { func (a *App) GetOAuthApp(appId string) (*model.OAuthApp, *model.AppError) {
if !*a.Config().ServiceSettings.EnableOAuthServiceProvider { if !*a.Config().ServiceSettings.EnableOAuthServiceProvider {
return nil, model.NewAppError("GetOAuthApp", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented) return nil, model.NewAppError("GetOAuthApp", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented)
} }
result := <-a.Srv.Store.OAuth().GetApp(appId) return a.Srv.Store.OAuth().GetApp(appId)
if result.Err != nil {
return nil, result.Err
}
return result.Data.(*model.OAuthApp), nil
} }
func (a *App) UpdateOauthApp(oldApp, updatedApp *model.OAuthApp) (*model.OAuthApp, *model.AppError) { func (a *App) UpdateOauthApp(oldApp, updatedApp *model.OAuthApp) (*model.OAuthApp, *model.AppError) {
@@ -64,12 +54,7 @@ func (a *App) UpdateOauthApp(oldApp, updatedApp *model.OAuthApp) (*model.OAuthAp
updatedApp.CreateAt = oldApp.CreateAt updatedApp.CreateAt = oldApp.CreateAt
updatedApp.ClientSecret = oldApp.ClientSecret updatedApp.ClientSecret = oldApp.ClientSecret
result := <-a.Srv.Store.OAuth().UpdateApp(updatedApp) return a.Srv.Store.OAuth().UpdateApp(updatedApp)
if result.Err != nil {
return nil, result.Err
}
return result.Data.([2]*model.OAuthApp)[0], nil
} }
func (a *App) DeleteOAuthApp(appId string) *model.AppError { func (a *App) DeleteOAuthApp(appId string) *model.AppError {
@@ -77,7 +62,7 @@ func (a *App) DeleteOAuthApp(appId string) *model.AppError {
return model.NewAppError("DeleteOAuthApp", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented) return model.NewAppError("DeleteOAuthApp", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented)
} }
if err := (<-a.Srv.Store.OAuth().DeleteApp(appId)).Err; err != nil { if err := a.Srv.Store.OAuth().DeleteApp(appId); err != nil {
return err return err
} }
@@ -93,12 +78,7 @@ func (a *App) GetOAuthApps(page, perPage int) ([]*model.OAuthApp, *model.AppErro
return nil, model.NewAppError("GetOAuthApps", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented) return nil, model.NewAppError("GetOAuthApps", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented)
} }
result := <-a.Srv.Store.OAuth().GetApps(page*perPage, perPage) return a.Srv.Store.OAuth().GetApps(page*perPage, perPage)
if result.Err != nil {
return nil, result.Err
}
return result.Data.([]*model.OAuthApp), nil
} }
func (a *App) GetOAuthAppsByCreator(userId string, page, perPage int) ([]*model.OAuthApp, *model.AppError) { func (a *App) GetOAuthAppsByCreator(userId string, page, perPage int) ([]*model.OAuthApp, *model.AppError) {
@@ -106,12 +86,7 @@ func (a *App) GetOAuthAppsByCreator(userId string, page, perPage int) ([]*model.
return nil, model.NewAppError("GetOAuthAppsByUser", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented) return nil, model.NewAppError("GetOAuthAppsByUser", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented)
} }
result := <-a.Srv.Store.OAuth().GetAppByUser(userId, page*perPage, perPage) return a.Srv.Store.OAuth().GetAppByUser(userId, page*perPage, perPage)
if result.Err != nil {
return nil, result.Err
}
return result.Data.([]*model.OAuthApp), nil
} }
func (a *App) GetOAuthImplicitRedirect(userId string, authRequest *model.AuthorizeRequest) (string, *model.AppError) { func (a *App) GetOAuthImplicitRedirect(userId string, authRequest *model.AuthorizeRequest) (string, *model.AppError) {
@@ -134,7 +109,7 @@ func (a *App) GetOAuthCodeRedirect(userId string, authRequest *model.AuthorizeRe
authData := &model.AuthData{UserId: userId, ClientId: authRequest.ClientId, CreateAt: model.GetMillis(), RedirectUri: authRequest.RedirectUri, State: authRequest.State, Scope: authRequest.Scope} authData := &model.AuthData{UserId: userId, ClientId: authRequest.ClientId, CreateAt: model.GetMillis(), RedirectUri: authRequest.RedirectUri, State: authRequest.State, Scope: authRequest.Scope}
authData.Code = model.NewId() + model.NewId() authData.Code = model.NewId() + model.NewId()
if result := <-a.Srv.Store.OAuth().SaveAuthData(authData); result.Err != nil { if _, err := a.Srv.Store.OAuth().SaveAuthData(authData); err != nil {
return authRequest.RedirectUri + "?error=server_error&state=" + authRequest.State, nil return authRequest.RedirectUri + "?error=server_error&state=" + authRequest.State, nil
} }
@@ -150,18 +125,16 @@ func (a *App) AllowOAuthAppAccessToUser(userId string, authRequest *model.Author
authRequest.Scope = model.DEFAULT_SCOPE authRequest.Scope = model.DEFAULT_SCOPE
} }
result := <-a.Srv.Store.OAuth().GetApp(authRequest.ClientId) oauthApp, err := a.Srv.Store.OAuth().GetApp(authRequest.ClientId)
if result.Err != nil { if err != nil {
return "", result.Err return "", err
} }
oauthApp := result.Data.(*model.OAuthApp)
if !oauthApp.IsValidRedirectURL(authRequest.RedirectUri) { if !oauthApp.IsValidRedirectURL(authRequest.RedirectUri) {
return "", model.NewAppError("AllowOAuthAppAccessToUser", "api.oauth.allow_oauth.redirect_callback.app_error", nil, "", http.StatusBadRequest) return "", model.NewAppError("AllowOAuthAppAccessToUser", "api.oauth.allow_oauth.redirect_callback.app_error", nil, "", http.StatusBadRequest)
} }
var redirectURI string var redirectURI string
var err *model.AppError
switch authRequest.ResponseType { switch authRequest.ResponseType {
case model.AUTHCODE_RESPONSE_TYPE: case model.AUTHCODE_RESPONSE_TYPE:
@@ -215,8 +188,8 @@ func (a *App) GetOAuthAccessTokenForImplicitFlow(userId string, authRequest *mod
accessData := &model.AccessData{ClientId: authRequest.ClientId, UserId: user.Id, Token: session.Token, RefreshToken: "", RedirectUri: authRequest.RedirectUri, ExpiresAt: session.ExpiresAt, Scope: authRequest.Scope} accessData := &model.AccessData{ClientId: authRequest.ClientId, UserId: user.Id, Token: session.Token, RefreshToken: "", RedirectUri: authRequest.RedirectUri, ExpiresAt: session.ExpiresAt, Scope: authRequest.Scope}
if result := <-a.Srv.Store.OAuth().SaveAccessData(accessData); result.Err != nil { if _, err := a.Srv.Store.OAuth().SaveAccessData(accessData); err != nil {
mlog.Error(fmt.Sprint(result.Err)) mlog.Error(fmt.Sprint(err))
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal_saving.app_error", nil, "", http.StatusInternalServerError) return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal_saving.app_error", nil, "", http.StatusInternalServerError)
} }
@@ -228,11 +201,10 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.disabled.app_error", nil, "", http.StatusNotImplemented) return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.disabled.app_error", nil, "", http.StatusNotImplemented)
} }
result := <-a.Srv.Store.OAuth().GetApp(clientId) oauthApp, err := a.Srv.Store.OAuth().GetApp(clientId)
if result.Err != nil { if err != nil {
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.credentials.app_error", nil, "", http.StatusNotFound) return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.credentials.app_error", nil, "", http.StatusNotFound)
} }
oauthApp := result.Data.(*model.OAuthApp)
if oauthApp.ClientSecret != secret { if oauthApp.ClientSecret != secret {
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.credentials.app_error", nil, "", http.StatusForbidden) return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.credentials.app_error", nil, "", http.StatusForbidden)
@@ -243,14 +215,13 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
var accessRsp *model.AccessResponse var accessRsp *model.AccessResponse
if grantType == model.ACCESS_TOKEN_GRANT_TYPE { if grantType == model.ACCESS_TOKEN_GRANT_TYPE {
var authData *model.AuthData var authData *model.AuthData
result := <-a.Srv.Store.OAuth().GetAuthData(code) authData, err = a.Srv.Store.OAuth().GetAuthData(code)
if result.Err != nil { if err != nil {
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.expired_code.app_error", nil, "", http.StatusBadRequest) return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.expired_code.app_error", nil, "", http.StatusBadRequest)
} }
authData = result.Data.(*model.AuthData)
if authData.IsExpired() { if authData.IsExpired() {
<-a.Srv.Store.OAuth().RemoveAuthData(authData.Code) a.Srv.Store.OAuth().RemoveAuthData(authData.Code)
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.expired_code.app_error", nil, "", http.StatusForbidden) return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.expired_code.app_error", nil, "", http.StatusForbidden)
} }
@@ -258,21 +229,20 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.redirect_uri.app_error", nil, "", http.StatusBadRequest) return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.redirect_uri.app_error", nil, "", http.StatusBadRequest)
} }
var err *model.AppError
user, err = a.Srv.Store.User().Get(authData.UserId) user, err = a.Srv.Store.User().Get(authData.UserId)
if err != nil { if err != nil {
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal_user.app_error", nil, "", http.StatusNotFound) return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal_user.app_error", nil, "", http.StatusNotFound)
} }
result = <-a.Srv.Store.OAuth().GetPreviousAccessData(user.Id, clientId) accessData, err = a.Srv.Store.OAuth().GetPreviousAccessData(user.Id, clientId)
if result.Err != nil { if err != nil {
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal.app_error", nil, "", http.StatusBadRequest) return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal.app_error", nil, "", http.StatusBadRequest)
} }
if result.Data != nil { if accessData != nil {
accessData := result.Data.(*model.AccessData)
if accessData.IsExpired() { if accessData.IsExpired() {
access, err := a.newSessionUpdateToken(oauthApp.Name, accessData, user) var access *model.AccessResponse
access, err = a.newSessionUpdateToken(oauthApp.Name, accessData, user)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -287,16 +257,17 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
} }
} }
} else { } else {
var session *model.Session
// Create a new session and return new access token // Create a new session and return new access token
session, err := a.newSession(oauthApp.Name, user) session, err = a.newSession(oauthApp.Name, user)
if err != nil { if err != nil {
return nil, err return nil, err
} }
accessData = &model.AccessData{ClientId: clientId, UserId: user.Id, Token: session.Token, RefreshToken: model.NewId(), RedirectUri: redirectUri, ExpiresAt: session.ExpiresAt, Scope: authData.Scope} accessData = &model.AccessData{ClientId: clientId, UserId: user.Id, Token: session.Token, RefreshToken: model.NewId(), RedirectUri: redirectUri, ExpiresAt: session.ExpiresAt, Scope: authData.Scope}
if result := <-a.Srv.Store.OAuth().SaveAccessData(accessData); result.Err != nil { if _, err = a.Srv.Store.OAuth().SaveAccessData(accessData); err != nil {
mlog.Error(fmt.Sprint(result.Err)) mlog.Error(fmt.Sprint(err))
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal_saving.app_error", nil, "", http.StatusInternalServerError) return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal_saving.app_error", nil, "", http.StatusInternalServerError)
} }
@@ -308,14 +279,13 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
} }
} }
<-a.Srv.Store.OAuth().RemoveAuthData(authData.Code) a.Srv.Store.OAuth().RemoveAuthData(authData.Code)
} else { } else {
// When grantType is refresh_token // When grantType is refresh_token
result := <-a.Srv.Store.OAuth().GetAccessDataByRefreshToken(refreshToken) accessData, err = a.Srv.Store.OAuth().GetAccessDataByRefreshToken(refreshToken)
if result.Err != nil { if err != nil {
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.refresh_token.app_error", nil, "", http.StatusNotFound) return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.refresh_token.app_error", nil, "", http.StatusNotFound)
} }
accessData = result.Data.(*model.AccessData)
user, err := a.Srv.Store.User().Get(accessData.UserId) user, err := a.Srv.Store.User().Get(accessData.UserId)
if err != nil { if err != nil {
@@ -366,8 +336,8 @@ func (a *App) newSessionUpdateToken(appName string, accessData *model.AccessData
accessData.RefreshToken = model.NewId() accessData.RefreshToken = model.NewId()
accessData.ExpiresAt = session.ExpiresAt accessData.ExpiresAt = session.ExpiresAt
if result := <-a.Srv.Store.OAuth().UpdateAccessData(accessData); result.Err != nil { if _, err := a.Srv.Store.OAuth().UpdateAccessData(accessData); err != nil {
mlog.Error(fmt.Sprint(result.Err)) mlog.Error(fmt.Sprint(err))
return nil, model.NewAppError("newSessionUpdateToken", "web.get_access_token.internal_saving.app_error", nil, "", http.StatusInternalServerError) return nil, model.NewAppError("newSessionUpdateToken", "web.get_access_token.internal_saving.app_error", nil, "", http.StatusInternalServerError)
} }
accessRsp := &model.AccessResponse{ accessRsp := &model.AccessResponse{
@@ -419,11 +389,10 @@ func (a *App) GetAuthorizedAppsForUser(userId string, page, perPage int) ([]*mod
return nil, model.NewAppError("GetAuthorizedAppsForUser", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented) return nil, model.NewAppError("GetAuthorizedAppsForUser", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented)
} }
result := <-a.Srv.Store.OAuth().GetAuthorizedApps(userId, page*perPage, perPage) apps, err := a.Srv.Store.OAuth().GetAuthorizedApps(userId, page*perPage, perPage)
if result.Err != nil { if err != nil {
return nil, result.Err return nil, err
} }
apps := result.Data.([]*model.OAuthApp)
for k, a := range apps { for k, a := range apps {
a.Sanitize() a.Sanitize()
@@ -439,19 +408,18 @@ func (a *App) DeauthorizeOAuthAppForUser(userId, appId string) *model.AppError {
} }
// Revoke app sessions // Revoke app sessions
result := <-a.Srv.Store.OAuth().GetAccessDataByUserForApp(userId, appId) accessData, err := a.Srv.Store.OAuth().GetAccessDataByUserForApp(userId, appId)
if result.Err != nil { if err != nil {
return result.Err return err
} }
accessData := result.Data.([]*model.AccessData)
for _, ad := range accessData { for _, ad := range accessData {
if err := a.RevokeAccessToken(ad.Token); err != nil { if err := a.RevokeAccessToken(ad.Token); err != nil {
return err return err
} }
if rad := <-a.Srv.Store.OAuth().RemoveAccessData(ad.Token); rad.Err != nil { if err := a.Srv.Store.OAuth().RemoveAccessData(ad.Token); err != nil {
return rad.Err return err
} }
} }
@@ -469,8 +437,8 @@ func (a *App) RegenerateOAuthAppSecret(app *model.OAuthApp) (*model.OAuthApp, *m
} }
app.ClientSecret = model.NewId() app.ClientSecret = model.NewId()
if update := <-a.Srv.Store.OAuth().UpdateApp(app); update.Err != nil { if _, err := a.Srv.Store.OAuth().UpdateApp(app); err != nil {
return nil, update.Err return nil, err
} }
return app, nil return app, nil
@@ -485,11 +453,11 @@ func (a *App) RevokeAccessToken(token string) *model.AppError {
close(schan) close(schan)
}() }()
if result := <-a.Srv.Store.OAuth().GetAccessData(token); result.Err != nil { if _, err := a.Srv.Store.OAuth().GetAccessData(token); err != nil {
return model.NewAppError("RevokeAccessToken", "api.oauth.revoke_access_token.get.app_error", nil, "", http.StatusBadRequest) return model.NewAppError("RevokeAccessToken", "api.oauth.revoke_access_token.get.app_error", nil, "", http.StatusBadRequest)
} }
if result := <-a.Srv.Store.OAuth().RemoveAccessData(token); result.Err != nil { if err := a.Srv.Store.OAuth().RemoveAccessData(token); err != nil {
return model.NewAppError("RevokeAccessToken", "api.oauth.revoke_access_token.del_token.app_error", nil, "", http.StatusInternalServerError) return model.NewAppError("RevokeAccessToken", "api.oauth.revoke_access_token.del_token.app_error", nil, "", http.StatusInternalServerError)
} }

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

@@ -92,11 +92,10 @@ func TestOAuthRevokeAccessToken(t *testing.T) {
accessData.ClientId = model.NewId() accessData.ClientId = model.NewId()
accessData.ExpiresAt = session.ExpiresAt accessData.ExpiresAt = session.ExpiresAt
if result := <-th.App.Srv.Store.OAuth().SaveAccessData(accessData); result.Err != nil { _, err := th.App.Srv.Store.OAuth().SaveAccessData(accessData)
t.Fatal(result.Err) require.Nil(t, err)
}
if err := th.App.RevokeAccessToken(accessData.Token); err != nil { if err = th.App.RevokeAccessToken(accessData.Token); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
@@ -136,15 +135,14 @@ func TestOAuthDeleteApp(t *testing.T) {
accessData.ClientId = a1.Id accessData.ClientId = a1.Id
accessData.ExpiresAt = session.ExpiresAt accessData.ExpiresAt = session.ExpiresAt
if result := <-th.App.Srv.Store.OAuth().SaveAccessData(accessData); result.Err != nil { _, err = th.App.Srv.Store.OAuth().SaveAccessData(accessData)
t.Fatal(result.Err) require.Nil(t, err)
}
if err := th.App.DeleteOAuthApp(a1.Id); err != nil { if err = th.App.DeleteOAuthApp(a1.Id); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if _, err := th.App.GetSession(session.Token); err == nil { if _, err = th.App.GetSession(session.Token); err == nil {
t.Fatal("should not get session from cache or db") t.Fatal("should not get session from cache or db")
} }
} }

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

@@ -1436,8 +1436,8 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
return result.Err return result.Err
} }
if result := <-a.Srv.Store.OAuth().PermanentDeleteAuthDataByUser(user.Id); result.Err != nil { if err := a.Srv.Store.OAuth().PermanentDeleteAuthDataByUser(user.Id); err != nil {
return result.Err return err
} }
if err := a.Srv.Store.Webhook().PermanentDeleteIncomingByUser(user.Id); err != nil { if err := a.Srv.Store.Webhook().PermanentDeleteIncomingByUser(user.Id); err != nil {

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

@@ -59,277 +59,234 @@ func (as SqlOAuthStore) CreateIndexesIfNotExists() {
as.CreateIndexIfNotExists("idx_oauthauthdata_client_id", "OAuthAuthData", "Code") as.CreateIndexIfNotExists("idx_oauthauthdata_client_id", "OAuthAuthData", "Code")
} }
func (as SqlOAuthStore) SaveApp(app *model.OAuthApp) store.StoreChannel { func (as SqlOAuthStore) SaveApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
if len(app.Id) > 0 { if len(app.Id) > 0 {
result.Err = model.NewAppError("SqlOAuthStore.SaveApp", "store.sql_oauth.save_app.existing.app_error", nil, "app_id="+app.Id, http.StatusBadRequest) return nil, model.NewAppError("SqlOAuthStore.SaveApp", "store.sql_oauth.save_app.existing.app_error", nil, "app_id="+app.Id, http.StatusBadRequest)
return
} }
app.PreSave() app.PreSave()
if result.Err = app.IsValid(); result.Err != nil { if err := app.IsValid(); err != nil {
return return nil, err
} }
if err := as.GetMaster().Insert(app); err != nil { if err := as.GetMaster().Insert(app); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.SaveApp", "store.sql_oauth.save_app.save.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlOAuthStore.SaveApp", "store.sql_oauth.save_app.save.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError)
} else {
result.Data = app
} }
}) return app, nil
} }
func (as SqlOAuthStore) UpdateApp(app *model.OAuthApp) store.StoreChannel { func (as SqlOAuthStore) UpdateApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
app.PreUpdate() app.PreUpdate()
if result.Err = app.IsValid(); result.Err != nil { if err := app.IsValid(); err != nil {
return return nil, err
}
oldAppResult, err := as.GetMaster().Get(model.OAuthApp{}, app.Id)
if err != nil {
return nil, model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.finding.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError)
}
if oldAppResult == nil {
return nil, model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.find.app_error", nil, "app_id="+app.Id, http.StatusBadRequest)
} }
if oldAppResult, err := as.GetMaster().Get(model.OAuthApp{}, app.Id); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.finding.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError)
} else if oldAppResult == nil {
result.Err = model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.find.app_error", nil, "app_id="+app.Id, http.StatusBadRequest)
} else {
oldApp := oldAppResult.(*model.OAuthApp) oldApp := oldAppResult.(*model.OAuthApp)
app.CreateAt = oldApp.CreateAt app.CreateAt = oldApp.CreateAt
app.CreatorId = oldApp.CreatorId app.CreatorId = oldApp.CreatorId
if count, err := as.GetMaster().Update(app); err != nil { count, err := as.GetMaster().Update(app)
result.Err = model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.updating.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError) if err != nil {
} else if count != 1 { return nil, model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.updating.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError)
result.Err = model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.update.app_error", nil, "app_id="+app.Id, http.StatusBadRequest)
} else {
result.Data = [2]*model.OAuthApp{app, oldApp}
} }
if count != 1 {
return nil, model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.update.app_error", nil, "app_id="+app.Id, http.StatusBadRequest)
} }
}) return app, nil
} }
func (as SqlOAuthStore) GetApp(id string) store.StoreChannel { func (as SqlOAuthStore) GetApp(id string) (*model.OAuthApp, *model.AppError) {
return store.Do(func(result *store.StoreResult) { obj, err := as.GetReplica().Get(model.OAuthApp{}, id)
if obj, err := as.GetReplica().Get(model.OAuthApp{}, id); err != nil { if err != nil {
result.Err = model.NewAppError("SqlOAuthStore.GetApp", "store.sql_oauth.get_app.finding.app_error", nil, "app_id="+id+", "+err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlOAuthStore.GetApp", "store.sql_oauth.get_app.finding.app_error", nil, "app_id="+id+", "+err.Error(), http.StatusInternalServerError)
} else if obj == nil {
result.Err = model.NewAppError("SqlOAuthStore.GetApp", "store.sql_oauth.get_app.find.app_error", nil, "app_id="+id, http.StatusNotFound)
} else {
result.Data = obj.(*model.OAuthApp)
} }
}) if obj == nil {
return nil, model.NewAppError("SqlOAuthStore.GetApp", "store.sql_oauth.get_app.find.app_error", nil, "app_id="+id, http.StatusNotFound)
}
return obj.(*model.OAuthApp), nil
} }
func (as SqlOAuthStore) GetAppByUser(userId string, offset, limit int) store.StoreChannel { func (as SqlOAuthStore) GetAppByUser(userId string, offset, limit int) ([]*model.OAuthApp, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
var apps []*model.OAuthApp var apps []*model.OAuthApp
if _, err := as.GetReplica().Select(&apps, "SELECT * FROM OAuthApps WHERE CreatorId = :UserId LIMIT :Limit OFFSET :Offset", map[string]interface{}{"UserId": userId, "Offset": offset, "Limit": limit}); err != nil { if _, err := as.GetReplica().Select(&apps, "SELECT * FROM OAuthApps WHERE CreatorId = :UserId LIMIT :Limit OFFSET :Offset", map[string]interface{}{"UserId": userId, "Offset": offset, "Limit": limit}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.GetAppByUser", "store.sql_oauth.get_app_by_user.find.app_error", nil, "user_id="+userId+", "+err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlOAuthStore.GetAppByUser", "store.sql_oauth.get_app_by_user.find.app_error", nil, "user_id="+userId+", "+err.Error(), http.StatusInternalServerError)
} }
result.Data = apps return apps, nil
})
} }
func (as SqlOAuthStore) GetApps(offset, limit int) store.StoreChannel { func (as SqlOAuthStore) GetApps(offset, limit int) ([]*model.OAuthApp, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
var apps []*model.OAuthApp var apps []*model.OAuthApp
if _, err := as.GetReplica().Select(&apps, "SELECT * FROM OAuthApps LIMIT :Limit OFFSET :Offset", map[string]interface{}{"Offset": offset, "Limit": limit}); err != nil { if _, err := as.GetReplica().Select(&apps, "SELECT * FROM OAuthApps LIMIT :Limit OFFSET :Offset", map[string]interface{}{"Offset": offset, "Limit": limit}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.GetAppByUser", "store.sql_oauth.get_apps.find.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlOAuthStore.GetAppByUser", "store.sql_oauth.get_apps.find.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
} }
result.Data = apps return apps, nil
})
} }
func (as SqlOAuthStore) GetAuthorizedApps(userId string, offset, limit int) store.StoreChannel { func (as SqlOAuthStore) GetAuthorizedApps(userId string, offset, limit int) ([]*model.OAuthApp, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
var apps []*model.OAuthApp var apps []*model.OAuthApp
if _, err := as.GetReplica().Select(&apps, if _, err := as.GetReplica().Select(&apps,
`SELECT o.* FROM OAuthApps AS o INNER JOIN `SELECT o.* FROM OAuthApps AS o INNER JOIN
Preferences AS p ON p.Name=o.Id AND p.UserId=:UserId LIMIT :Limit OFFSET :Offset`, map[string]interface{}{"UserId": userId, "Offset": offset, "Limit": limit}); err != nil { Preferences AS p ON p.Name=o.Id AND p.UserId=:UserId LIMIT :Limit OFFSET :Offset`, map[string]interface{}{"UserId": userId, "Offset": offset, "Limit": limit}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.GetAuthorizedApps", "store.sql_oauth.get_apps.find.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlOAuthStore.GetAuthorizedApps", "store.sql_oauth.get_apps.find.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
} }
result.Data = apps return apps, nil
})
} }
func (as SqlOAuthStore) DeleteApp(id string) store.StoreChannel { func (as SqlOAuthStore) DeleteApp(id string) *model.AppError {
return store.Do(func(result *store.StoreResult) {
// wrap in a transaction so that if one fails, everything fails // wrap in a transaction so that if one fails, everything fails
transaction, err := as.GetMaster().Begin() transaction, err := as.GetMaster().Begin()
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) return model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
} else { }
defer finalizeTransaction(transaction) defer finalizeTransaction(transaction)
if extrasResult := as.deleteApp(transaction, id); extrasResult.Err != nil {
*result = extrasResult if err := as.deleteApp(transaction, id); err != nil {
return err
} }
if result.Err == nil {
if err := transaction.Commit(); err != nil { if err := transaction.Commit(); err != nil {
// don't need to rollback here since the transaction is already closed // don't need to rollback here since the transaction is already closed
result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) return model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
} }
} return nil
}
})
} }
func (as SqlOAuthStore) SaveAccessData(accessData *model.AccessData) store.StoreChannel { func (as SqlOAuthStore) SaveAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError) {
return store.Do(func(result *store.StoreResult) { if err := accessData.IsValid(); err != nil {
if result.Err = accessData.IsValid(); result.Err != nil { return nil, err
return
} }
if err := as.GetMaster().Insert(accessData); err != nil { if err := as.GetMaster().Insert(accessData); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.SaveAccessData", "store.sql_oauth.save_access_data.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlOAuthStore.SaveAccessData", "store.sql_oauth.save_access_data.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = accessData
} }
}) return accessData, nil
} }
func (as SqlOAuthStore) GetAccessData(token string) store.StoreChannel { func (as SqlOAuthStore) GetAccessData(token string) (*model.AccessData, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
accessData := model.AccessData{} accessData := model.AccessData{}
if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil { if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.GetAccessData", "store.sql_oauth.get_access_data.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlOAuthStore.GetAccessData", "store.sql_oauth.get_access_data.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = &accessData
} }
}) return &accessData, nil
} }
func (as SqlOAuthStore) GetAccessDataByUserForApp(userId, clientId string) store.StoreChannel { func (as SqlOAuthStore) GetAccessDataByUserForApp(userId, clientId string) ([]*model.AccessData, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
var accessData []*model.AccessData var accessData []*model.AccessData
if _, err := as.GetReplica().Select(&accessData, if _, err := as.GetReplica().Select(&accessData,
"SELECT * FROM OAuthAccessData WHERE UserId = :UserId AND ClientId = :ClientId", "SELECT * FROM OAuthAccessData WHERE UserId = :UserId AND ClientId = :ClientId",
map[string]interface{}{"UserId": userId, "ClientId": clientId}); err != nil { map[string]interface{}{"UserId": userId, "ClientId": clientId}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.GetAccessDataByUserForApp", "store.sql_oauth.get_access_data_by_user_for_app.app_error", nil, "user_id="+userId+" client_id="+clientId, http.StatusInternalServerError) return nil, model.NewAppError("SqlOAuthStore.GetAccessDataByUserForApp", "store.sql_oauth.get_access_data_by_user_for_app.app_error", nil, "user_id="+userId+" client_id="+clientId, http.StatusInternalServerError)
} else {
result.Data = accessData
} }
}) return accessData, nil
} }
func (as SqlOAuthStore) GetAccessDataByRefreshToken(token string) store.StoreChannel { func (as SqlOAuthStore) GetAccessDataByRefreshToken(token string) (*model.AccessData, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
accessData := model.AccessData{} accessData := model.AccessData{}
if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE RefreshToken = :Token", map[string]interface{}{"Token": token}); err != nil { if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE RefreshToken = :Token", map[string]interface{}{"Token": token}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.GetAccessData", "store.sql_oauth.get_access_data.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlOAuthStore.GetAccessData", "store.sql_oauth.get_access_data.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = &accessData
} }
}) return &accessData, nil
} }
func (as SqlOAuthStore) GetPreviousAccessData(userId, clientId string) store.StoreChannel { func (as SqlOAuthStore) GetPreviousAccessData(userId, clientId string) (*model.AccessData, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
accessData := model.AccessData{} accessData := model.AccessData{}
if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE ClientId = :ClientId AND UserId = :UserId", if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE ClientId = :ClientId AND UserId = :UserId",
map[string]interface{}{"ClientId": clientId, "UserId": userId}); err != nil { map[string]interface{}{"ClientId": clientId, "UserId": userId}); err != nil {
if strings.Contains(err.Error(), "no rows") { if strings.Contains(err.Error(), "no rows") {
result.Data = nil return nil, nil
} else {
result.Err = model.NewAppError("SqlOAuthStore.GetPreviousAccessData", "store.sql_oauth.get_previous_access_data.app_error", nil, err.Error(), http.StatusNotFound)
} }
} else { return nil, model.NewAppError("SqlOAuthStore.GetPreviousAccessData", "store.sql_oauth.get_previous_access_data.app_error", nil, err.Error(), http.StatusNotFound)
result.Data = &accessData
} }
}) return &accessData, nil
} }
func (as SqlOAuthStore) UpdateAccessData(accessData *model.AccessData) store.StoreChannel { func (as SqlOAuthStore) UpdateAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError) {
return store.Do(func(result *store.StoreResult) { if err := accessData.IsValid(); err != nil {
if result.Err = accessData.IsValid(); result.Err != nil { return nil, err
return
} }
if _, err := as.GetMaster().Exec("UPDATE OAuthAccessData SET Token = :Token, ExpiresAt = :ExpiresAt, RefreshToken = :RefreshToken WHERE ClientId = :ClientId AND UserID = :UserId", if _, err := as.GetMaster().Exec("UPDATE OAuthAccessData SET Token = :Token, ExpiresAt = :ExpiresAt, RefreshToken = :RefreshToken WHERE ClientId = :ClientId AND UserID = :UserId",
map[string]interface{}{"Token": accessData.Token, "ExpiresAt": accessData.ExpiresAt, "RefreshToken": accessData.RefreshToken, "ClientId": accessData.ClientId, "UserId": accessData.UserId}); err != nil { map[string]interface{}{"Token": accessData.Token, "ExpiresAt": accessData.ExpiresAt, "RefreshToken": accessData.RefreshToken, "ClientId": accessData.ClientId, "UserId": accessData.UserId}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.Update", "store.sql_oauth.update_access_data.app_error", nil, return nil, model.NewAppError("SqlOAuthStore.Update", "store.sql_oauth.update_access_data.app_error", nil,
"clientId="+accessData.ClientId+",userId="+accessData.UserId+", "+err.Error(), http.StatusInternalServerError) "clientId="+accessData.ClientId+",userId="+accessData.UserId+", "+err.Error(), http.StatusInternalServerError)
} else {
result.Data = accessData
} }
}) return accessData, nil
} }
func (as SqlOAuthStore) RemoveAccessData(token string) store.StoreChannel { func (as SqlOAuthStore) RemoveAccessData(token string) *model.AppError {
return store.Do(func(result *store.StoreResult) {
if _, err := as.GetMaster().Exec("DELETE FROM OAuthAccessData WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil { if _, err := as.GetMaster().Exec("DELETE FROM OAuthAccessData WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.RemoveAccessData", "store.sql_oauth.remove_access_data.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) return model.NewAppError("SqlOAuthStore.RemoveAccessData", "store.sql_oauth.remove_access_data.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
} }
}) return nil
} }
func (as SqlOAuthStore) SaveAuthData(authData *model.AuthData) store.StoreChannel { func (as SqlOAuthStore) SaveAuthData(authData *model.AuthData) (*model.AuthData, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
authData.PreSave() authData.PreSave()
if result.Err = authData.IsValid(); result.Err != nil { if err := authData.IsValid(); err != nil {
return return nil, err
} }
if err := as.GetMaster().Insert(authData); err != nil { if err := as.GetMaster().Insert(authData); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.SaveAuthData", "store.sql_oauth.save_auth_data.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlOAuthStore.SaveAuthData", "store.sql_oauth.save_auth_data.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = authData
} }
}) return authData, nil
} }
func (as SqlOAuthStore) GetAuthData(code string) store.StoreChannel { func (as SqlOAuthStore) GetAuthData(code string) (*model.AuthData, *model.AppError) {
return store.Do(func(result *store.StoreResult) { obj, err := as.GetReplica().Get(model.AuthData{}, code)
if obj, err := as.GetReplica().Get(model.AuthData{}, code); err != nil { if err != nil {
result.Err = model.NewAppError("SqlOAuthStore.GetAuthData", "store.sql_oauth.get_auth_data.finding.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlOAuthStore.GetAuthData", "store.sql_oauth.get_auth_data.finding.app_error", nil, err.Error(), http.StatusInternalServerError)
} else if obj == nil {
result.Err = model.NewAppError("SqlOAuthStore.GetAuthData", "store.sql_oauth.get_auth_data.find.app_error", nil, "", http.StatusNotFound)
} else {
result.Data = obj.(*model.AuthData)
} }
}) if obj == nil {
return nil, model.NewAppError("SqlOAuthStore.GetAuthData", "store.sql_oauth.get_auth_data.find.app_error", nil, "", http.StatusNotFound)
}
return obj.(*model.AuthData), nil
} }
func (as SqlOAuthStore) RemoveAuthData(code string) store.StoreChannel { func (as SqlOAuthStore) RemoveAuthData(code string) *model.AppError {
return store.Do(func(result *store.StoreResult) {
_, err := as.GetMaster().Exec("DELETE FROM OAuthAuthData WHERE Code = :Code", map[string]interface{}{"Code": code}) _, err := as.GetMaster().Exec("DELETE FROM OAuthAuthData WHERE Code = :Code", map[string]interface{}{"Code": code})
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlOAuthStore.RemoveAuthData", "store.sql_oauth.remove_auth_data.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) return model.NewAppError("SqlOAuthStore.RemoveAuthData", "store.sql_oauth.remove_auth_data.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
} }
}) return nil
} }
func (as SqlOAuthStore) PermanentDeleteAuthDataByUser(userId string) store.StoreChannel { func (as SqlOAuthStore) PermanentDeleteAuthDataByUser(userId string) *model.AppError {
return store.Do(func(result *store.StoreResult) {
_, err := as.GetMaster().Exec("DELETE FROM OAuthAccessData WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}) _, err := as.GetMaster().Exec("DELETE FROM OAuthAccessData WHERE UserId = :UserId", map[string]interface{}{"UserId": userId})
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlOAuthStore.RemoveAuthDataByUserId", "store.sql_oauth.permanent_delete_auth_data_by_user.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) return model.NewAppError("SqlOAuthStore.RemoveAuthDataByUserId", "store.sql_oauth.permanent_delete_auth_data_by_user.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
} }
}) return nil
} }
func (as SqlOAuthStore) deleteApp(transaction *gorp.Transaction, clientId string) store.StoreResult { func (as SqlOAuthStore) deleteApp(transaction *gorp.Transaction, clientId string) *model.AppError {
result := store.StoreResult{}
if _, err := transaction.Exec("DELETE FROM OAuthApps WHERE Id = :Id", map[string]interface{}{"Id": clientId}); err != nil { if _, err := transaction.Exec("DELETE FROM OAuthApps WHERE Id = :Id", map[string]interface{}{"Id": clientId}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError) return model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError)
return result
} }
return as.deleteOAuthAppSessions(transaction, clientId) return as.deleteOAuthAppSessions(transaction, clientId)
} }
func (as SqlOAuthStore) deleteOAuthAppSessions(transaction *gorp.Transaction, clientId string) store.StoreResult { func (as SqlOAuthStore) deleteOAuthAppSessions(transaction *gorp.Transaction, clientId string) *model.AppError {
result := store.StoreResult{}
query := "" query := ""
if as.DriverName() == model.DATABASE_DRIVER_POSTGRES { if as.DriverName() == model.DATABASE_DRIVER_POSTGRES {
@@ -339,36 +296,29 @@ func (as SqlOAuthStore) deleteOAuthAppSessions(transaction *gorp.Transaction, cl
} }
if _, err := transaction.Exec(query, map[string]interface{}{"Id": clientId}); err != nil { if _, err := transaction.Exec(query, map[string]interface{}{"Id": clientId}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError) return model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError)
return result
} }
return as.deleteOAuthTokens(transaction, clientId) return as.deleteOAuthTokens(transaction, clientId)
} }
func (as SqlOAuthStore) deleteOAuthTokens(transaction *gorp.Transaction, clientId string) store.StoreResult { func (as SqlOAuthStore) deleteOAuthTokens(transaction *gorp.Transaction, clientId string) *model.AppError {
result := store.StoreResult{}
if _, err := transaction.Exec("DELETE FROM OAuthAccessData WHERE ClientId = :Id", map[string]interface{}{"Id": clientId}); err != nil { if _, err := transaction.Exec("DELETE FROM OAuthAccessData WHERE ClientId = :Id", map[string]interface{}{"Id": clientId}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError) return model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError)
return result
} }
return as.deleteAppExtras(transaction, clientId) return as.deleteAppExtras(transaction, clientId)
} }
func (as SqlOAuthStore) deleteAppExtras(transaction *gorp.Transaction, clientId string) store.StoreResult { func (as SqlOAuthStore) deleteAppExtras(transaction *gorp.Transaction, clientId string) *model.AppError {
result := store.StoreResult{}
if _, err := transaction.Exec( if _, err := transaction.Exec(
`DELETE FROM `DELETE FROM
Preferences Preferences
WHERE WHERE
Category = :Category Category = :Category
AND Name = :Name`, map[string]interface{}{"Category": model.PREFERENCE_CATEGORY_AUTHORIZED_OAUTH_APP, "Name": clientId}); err != nil { AND Name = :Name`, map[string]interface{}{"Category": model.PREFERENCE_CATEGORY_AUTHORIZED_OAUTH_APP, "Name": clientId}); err != nil {
result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_preference.delete.app_error", nil, err.Error(), http.StatusInternalServerError) return model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_preference.delete.app_error", nil, err.Error(), http.StatusInternalServerError)
return result
} }
return result return nil
} }

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

@@ -352,24 +352,24 @@ type ComplianceStore interface {
} }
type OAuthStore interface { type OAuthStore interface {
SaveApp(app *model.OAuthApp) StoreChannel SaveApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError)
UpdateApp(app *model.OAuthApp) StoreChannel UpdateApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError)
GetApp(id string) StoreChannel GetApp(id string) (*model.OAuthApp, *model.AppError)
GetAppByUser(userId string, offset, limit int) StoreChannel GetAppByUser(userId string, offset, limit int) ([]*model.OAuthApp, *model.AppError)
GetApps(offset, limit int) StoreChannel GetApps(offset, limit int) ([]*model.OAuthApp, *model.AppError)
GetAuthorizedApps(userId string, offset, limit int) StoreChannel GetAuthorizedApps(userId string, offset, limit int) ([]*model.OAuthApp, *model.AppError)
DeleteApp(id string) StoreChannel DeleteApp(id string) *model.AppError
SaveAuthData(authData *model.AuthData) StoreChannel SaveAuthData(authData *model.AuthData) (*model.AuthData, *model.AppError)
GetAuthData(code string) StoreChannel GetAuthData(code string) (*model.AuthData, *model.AppError)
RemoveAuthData(code string) StoreChannel RemoveAuthData(code string) *model.AppError
PermanentDeleteAuthDataByUser(userId string) StoreChannel PermanentDeleteAuthDataByUser(userId string) *model.AppError
SaveAccessData(accessData *model.AccessData) StoreChannel SaveAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError)
UpdateAccessData(accessData *model.AccessData) StoreChannel UpdateAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError)
GetAccessData(token string) StoreChannel GetAccessData(token string) (*model.AccessData, *model.AppError)
GetAccessDataByUserForApp(userId, clientId string) StoreChannel GetAccessDataByUserForApp(userId, clientId string) ([]*model.AccessData, *model.AppError)
GetAccessDataByRefreshToken(token string) StoreChannel GetAccessDataByRefreshToken(token string) (*model.AccessData, *model.AppError)
GetPreviousAccessData(userId, clientId string) StoreChannel GetPreviousAccessData(userId, clientId string) (*model.AccessData, *model.AppError)
RemoveAccessData(token string) StoreChannel RemoveAccessData(token string) *model.AppError
} }
type SystemStore interface { type SystemStore interface {

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

@@ -6,7 +6,6 @@ package mocks
import mock "github.com/stretchr/testify/mock" import mock "github.com/stretchr/testify/mock"
import model "github.com/mattermost/mattermost-server/model" import model "github.com/mattermost/mattermost-server/model"
import store "github.com/mattermost/mattermost-server/store"
// OAuthStore is an autogenerated mock type for the OAuthStore type // OAuthStore is an autogenerated mock type for the OAuthStore type
type OAuthStore struct { type OAuthStore struct {
@@ -14,15 +13,15 @@ type OAuthStore struct {
} }
// DeleteApp provides a mock function with given fields: id // DeleteApp provides a mock function with given fields: id
func (_m *OAuthStore) DeleteApp(id string) store.StoreChannel { func (_m *OAuthStore) DeleteApp(id string) *model.AppError {
ret := _m.Called(id) ret := _m.Called(id)
var r0 store.StoreChannel var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string) *model.AppError); ok {
r0 = rf(id) r0 = rf(id)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AppError)
} }
} }
@@ -30,159 +29,240 @@ func (_m *OAuthStore) DeleteApp(id string) store.StoreChannel {
} }
// GetAccessData provides a mock function with given fields: token // GetAccessData provides a mock function with given fields: token
func (_m *OAuthStore) GetAccessData(token string) store.StoreChannel { func (_m *OAuthStore) GetAccessData(token string) (*model.AccessData, *model.AppError) {
ret := _m.Called(token) ret := _m.Called(token)
var r0 store.StoreChannel var r0 *model.AccessData
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string) *model.AccessData); ok {
r0 = rf(token) r0 = rf(token)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AccessData)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
r1 = rf(token)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetAccessDataByRefreshToken provides a mock function with given fields: token // GetAccessDataByRefreshToken provides a mock function with given fields: token
func (_m *OAuthStore) GetAccessDataByRefreshToken(token string) store.StoreChannel { func (_m *OAuthStore) GetAccessDataByRefreshToken(token string) (*model.AccessData, *model.AppError) {
ret := _m.Called(token) ret := _m.Called(token)
var r0 store.StoreChannel var r0 *model.AccessData
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string) *model.AccessData); ok {
r0 = rf(token) r0 = rf(token)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AccessData)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
r1 = rf(token)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetAccessDataByUserForApp provides a mock function with given fields: userId, clientId // GetAccessDataByUserForApp provides a mock function with given fields: userId, clientId
func (_m *OAuthStore) GetAccessDataByUserForApp(userId string, clientId string) store.StoreChannel { func (_m *OAuthStore) GetAccessDataByUserForApp(userId string, clientId string) ([]*model.AccessData, *model.AppError) {
ret := _m.Called(userId, clientId) ret := _m.Called(userId, clientId)
var r0 store.StoreChannel var r0 []*model.AccessData
if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, string) []*model.AccessData); ok {
r0 = rf(userId, clientId) r0 = rf(userId, clientId)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).([]*model.AccessData)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, string) *model.AppError); ok {
r1 = rf(userId, clientId)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetApp provides a mock function with given fields: id // GetApp provides a mock function with given fields: id
func (_m *OAuthStore) GetApp(id string) store.StoreChannel { func (_m *OAuthStore) GetApp(id string) (*model.OAuthApp, *model.AppError) {
ret := _m.Called(id) ret := _m.Called(id)
var r0 store.StoreChannel var r0 *model.OAuthApp
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string) *model.OAuthApp); ok {
r0 = rf(id) r0 = rf(id)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.OAuthApp)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
r1 = rf(id)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetAppByUser provides a mock function with given fields: userId, offset, limit // GetAppByUser provides a mock function with given fields: userId, offset, limit
func (_m *OAuthStore) GetAppByUser(userId string, offset int, limit int) store.StoreChannel { func (_m *OAuthStore) GetAppByUser(userId string, offset int, limit int) ([]*model.OAuthApp, *model.AppError) {
ret := _m.Called(userId, offset, limit) ret := _m.Called(userId, offset, limit)
var r0 store.StoreChannel var r0 []*model.OAuthApp
if rf, ok := ret.Get(0).(func(string, int, int) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, int, int) []*model.OAuthApp); ok {
r0 = rf(userId, offset, limit) r0 = rf(userId, offset, limit)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).([]*model.OAuthApp)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, int, int) *model.AppError); ok {
r1 = rf(userId, offset, limit)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetApps provides a mock function with given fields: offset, limit // GetApps provides a mock function with given fields: offset, limit
func (_m *OAuthStore) GetApps(offset int, limit int) store.StoreChannel { func (_m *OAuthStore) GetApps(offset int, limit int) ([]*model.OAuthApp, *model.AppError) {
ret := _m.Called(offset, limit) ret := _m.Called(offset, limit)
var r0 store.StoreChannel var r0 []*model.OAuthApp
if rf, ok := ret.Get(0).(func(int, int) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(int, int) []*model.OAuthApp); ok {
r0 = rf(offset, limit) r0 = rf(offset, limit)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).([]*model.OAuthApp)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(int, int) *model.AppError); ok {
r1 = rf(offset, limit)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetAuthData provides a mock function with given fields: code // GetAuthData provides a mock function with given fields: code
func (_m *OAuthStore) GetAuthData(code string) store.StoreChannel { func (_m *OAuthStore) GetAuthData(code string) (*model.AuthData, *model.AppError) {
ret := _m.Called(code) ret := _m.Called(code)
var r0 store.StoreChannel var r0 *model.AuthData
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string) *model.AuthData); ok {
r0 = rf(code) r0 = rf(code)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AuthData)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
r1 = rf(code)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetAuthorizedApps provides a mock function with given fields: userId, offset, limit // GetAuthorizedApps provides a mock function with given fields: userId, offset, limit
func (_m *OAuthStore) GetAuthorizedApps(userId string, offset int, limit int) store.StoreChannel { func (_m *OAuthStore) GetAuthorizedApps(userId string, offset int, limit int) ([]*model.OAuthApp, *model.AppError) {
ret := _m.Called(userId, offset, limit) ret := _m.Called(userId, offset, limit)
var r0 store.StoreChannel var r0 []*model.OAuthApp
if rf, ok := ret.Get(0).(func(string, int, int) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, int, int) []*model.OAuthApp); ok {
r0 = rf(userId, offset, limit) r0 = rf(userId, offset, limit)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).([]*model.OAuthApp)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, int, int) *model.AppError); ok {
r1 = rf(userId, offset, limit)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetPreviousAccessData provides a mock function with given fields: userId, clientId // GetPreviousAccessData provides a mock function with given fields: userId, clientId
func (_m *OAuthStore) GetPreviousAccessData(userId string, clientId string) store.StoreChannel { func (_m *OAuthStore) GetPreviousAccessData(userId string, clientId string) (*model.AccessData, *model.AppError) {
ret := _m.Called(userId, clientId) ret := _m.Called(userId, clientId)
var r0 store.StoreChannel var r0 *model.AccessData
if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, string) *model.AccessData); ok {
r0 = rf(userId, clientId) r0 = rf(userId, clientId)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AccessData)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, string) *model.AppError); ok {
r1 = rf(userId, clientId)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// PermanentDeleteAuthDataByUser provides a mock function with given fields: userId // PermanentDeleteAuthDataByUser provides a mock function with given fields: userId
func (_m *OAuthStore) PermanentDeleteAuthDataByUser(userId string) store.StoreChannel { func (_m *OAuthStore) PermanentDeleteAuthDataByUser(userId string) *model.AppError {
ret := _m.Called(userId) ret := _m.Called(userId)
var r0 store.StoreChannel var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string) *model.AppError); ok {
r0 = rf(userId) r0 = rf(userId)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AppError)
} }
} }
@@ -190,15 +270,15 @@ func (_m *OAuthStore) PermanentDeleteAuthDataByUser(userId string) store.StoreCh
} }
// RemoveAccessData provides a mock function with given fields: token // RemoveAccessData provides a mock function with given fields: token
func (_m *OAuthStore) RemoveAccessData(token string) store.StoreChannel { func (_m *OAuthStore) RemoveAccessData(token string) *model.AppError {
ret := _m.Called(token) ret := _m.Called(token)
var r0 store.StoreChannel var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string) *model.AppError); ok {
r0 = rf(token) r0 = rf(token)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AppError)
} }
} }
@@ -206,15 +286,15 @@ func (_m *OAuthStore) RemoveAccessData(token string) store.StoreChannel {
} }
// RemoveAuthData provides a mock function with given fields: code // RemoveAuthData provides a mock function with given fields: code
func (_m *OAuthStore) RemoveAuthData(code string) store.StoreChannel { func (_m *OAuthStore) RemoveAuthData(code string) *model.AppError {
ret := _m.Called(code) ret := _m.Called(code)
var r0 store.StoreChannel var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string) *model.AppError); ok {
r0 = rf(code) r0 = rf(code)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AppError)
} }
} }
@@ -222,81 +302,126 @@ func (_m *OAuthStore) RemoveAuthData(code string) store.StoreChannel {
} }
// SaveAccessData provides a mock function with given fields: accessData // SaveAccessData provides a mock function with given fields: accessData
func (_m *OAuthStore) SaveAccessData(accessData *model.AccessData) store.StoreChannel { func (_m *OAuthStore) SaveAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError) {
ret := _m.Called(accessData) ret := _m.Called(accessData)
var r0 store.StoreChannel var r0 *model.AccessData
if rf, ok := ret.Get(0).(func(*model.AccessData) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(*model.AccessData) *model.AccessData); ok {
r0 = rf(accessData) r0 = rf(accessData)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AccessData)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.AccessData) *model.AppError); ok {
r1 = rf(accessData)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// SaveApp provides a mock function with given fields: app // SaveApp provides a mock function with given fields: app
func (_m *OAuthStore) SaveApp(app *model.OAuthApp) store.StoreChannel { func (_m *OAuthStore) SaveApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError) {
ret := _m.Called(app) ret := _m.Called(app)
var r0 store.StoreChannel var r0 *model.OAuthApp
if rf, ok := ret.Get(0).(func(*model.OAuthApp) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(*model.OAuthApp) *model.OAuthApp); ok {
r0 = rf(app) r0 = rf(app)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.OAuthApp)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.OAuthApp) *model.AppError); ok {
r1 = rf(app)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// SaveAuthData provides a mock function with given fields: authData // SaveAuthData provides a mock function with given fields: authData
func (_m *OAuthStore) SaveAuthData(authData *model.AuthData) store.StoreChannel { func (_m *OAuthStore) SaveAuthData(authData *model.AuthData) (*model.AuthData, *model.AppError) {
ret := _m.Called(authData) ret := _m.Called(authData)
var r0 store.StoreChannel var r0 *model.AuthData
if rf, ok := ret.Get(0).(func(*model.AuthData) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(*model.AuthData) *model.AuthData); ok {
r0 = rf(authData) r0 = rf(authData)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AuthData)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.AuthData) *model.AppError); ok {
r1 = rf(authData)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// UpdateAccessData provides a mock function with given fields: accessData // UpdateAccessData provides a mock function with given fields: accessData
func (_m *OAuthStore) UpdateAccessData(accessData *model.AccessData) store.StoreChannel { func (_m *OAuthStore) UpdateAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError) {
ret := _m.Called(accessData) ret := _m.Called(accessData)
var r0 store.StoreChannel var r0 *model.AccessData
if rf, ok := ret.Get(0).(func(*model.AccessData) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(*model.AccessData) *model.AccessData); ok {
r0 = rf(accessData) r0 = rf(accessData)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AccessData)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.AccessData) *model.AppError); ok {
r1 = rf(accessData)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// UpdateApp provides a mock function with given fields: app // UpdateApp provides a mock function with given fields: app
func (_m *OAuthStore) UpdateApp(app *model.OAuthApp) store.StoreChannel { func (_m *OAuthStore) UpdateApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError) {
ret := _m.Called(app) ret := _m.Called(app)
var r0 store.StoreChannel var r0 *model.OAuthApp
if rf, ok := ret.Get(0).(func(*model.OAuthApp) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(*model.OAuthApp) *model.OAuthApp); ok {
r0 = rf(app) r0 = rf(app)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.OAuthApp)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.OAuthApp) *model.AppError); ok {
r1 = rf(app)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }

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

@@ -8,6 +8,7 @@ import (
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store" "github.com/mattermost/mattermost-server/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@@ -36,22 +37,19 @@ func testOAuthStoreSaveApp(t *testing.T, ss store.Store) {
// Try to save an app that already has an Id // Try to save an app that already has an Id
a1.Id = model.NewId() a1.Id = model.NewId()
if err := (<-ss.OAuth().SaveApp(&a1)).Err; err == nil { _, err := ss.OAuth().SaveApp(&a1)
t.Fatal("Should have failed, cannot add an OAuth app cannot be save with an Id, it has to be updated") require.NotNil(t, err, "Should have failed, cannot add an OAuth app cannot be save with an Id, it has to be updated")
}
// Try to save an Invalid App // Try to save an Invalid App
a1.Id = "" a1.Id = ""
if err := (<-ss.OAuth().SaveApp(&a1)).Err; err == nil { _, err = ss.OAuth().SaveApp(&a1)
t.Fatal("Should have failed, app should be invalid cause it doesn' have a name set") require.NotNil(t, err, "Should have failed, app should be invalid cause it doesn' have a name set")
}
// Save the app // Save the app
a1.Id = "" a1.Id = ""
a1.Name = "TestApp" + model.NewId() a1.Name = "TestApp" + model.NewId()
if err := (<-ss.OAuth().SaveApp(&a1)).Err; err != nil { _, err = ss.OAuth().SaveApp(&a1)
t.Fatal(err) require.Nil(t, err)
}
} }
func testOAuthStoreGetApp(t *testing.T, ss store.Store) { func testOAuthStoreGetApp(t *testing.T, ss store.Store) {
@@ -60,33 +58,26 @@ func testOAuthStoreGetApp(t *testing.T, ss store.Store) {
a1.Name = "TestApp" + model.NewId() a1.Name = "TestApp" + model.NewId()
a1.CallbackUrls = []string{"https://nowhere.com"} a1.CallbackUrls = []string{"https://nowhere.com"}
a1.Homepage = "https://nowhere.com" a1.Homepage = "https://nowhere.com"
store.Must(ss.OAuth().SaveApp(&a1)) _, err := ss.OAuth().SaveApp(&a1)
require.Nil(t, err)
// Lets try to get and app that does not exists // Lets try to get and app that does not exists
if err := (<-ss.OAuth().GetApp("fake0123456789abcderfgret1")).Err; err == nil { _, err = ss.OAuth().GetApp("fake0123456789abcderfgret1")
t.Fatal("Should have failed. App does not exists") require.NotNil(t, err, "Should have failed. App does not exists")
}
if err := (<-ss.OAuth().GetApp(a1.Id)).Err; err != nil { _, err = ss.OAuth().GetApp(a1.Id)
t.Fatal(err) require.Nil(t, err)
}
// Lets try and get the app from a user that hasn't created any apps // Lets try and get the app from a user that hasn't created any apps
if result := (<-ss.OAuth().GetAppByUser("fake0123456789abcderfgret1", 0, 1000)); result.Err == nil { apps, err := ss.OAuth().GetAppByUser("fake0123456789abcderfgret1", 0, 1000)
if len(result.Data.([]*model.OAuthApp)) > 0 { require.Nil(t, err)
t.Fatal("Should have failed. Fake user hasn't created any apps") assert.Len(t, apps, 0, "Should have failed. Fake user hasn't created any apps")
}
} else {
t.Fatal(result.Err)
}
if err := (<-ss.OAuth().GetAppByUser(a1.CreatorId, 0, 1000)).Err; err != nil { _, err = ss.OAuth().GetAppByUser(a1.CreatorId, 0, 1000)
t.Fatal(err) require.Nil(t, err)
}
if err := (<-ss.OAuth().GetApps(0, 1000)).Err; err != nil { _, err = ss.OAuth().GetApps(0, 1000)
t.Fatal(err) require.Nil(t, err)
}
} }
func testOAuthStoreUpdateApp(t *testing.T, ss store.Store) { func testOAuthStoreUpdateApp(t *testing.T, ss store.Store) {
@@ -95,7 +86,8 @@ func testOAuthStoreUpdateApp(t *testing.T, ss store.Store) {
a1.Name = "TestApp" + model.NewId() a1.Name = "TestApp" + model.NewId()
a1.CallbackUrls = []string{"https://nowhere.com"} a1.CallbackUrls = []string{"https://nowhere.com"}
a1.Homepage = "https://nowhere.com" a1.Homepage = "https://nowhere.com"
store.Must(ss.OAuth().SaveApp(&a1)) _, err := ss.OAuth().SaveApp(&a1)
require.Nil(t, err)
// temporarily save the created app id // temporarily save the created app id
id := a1.Id id := a1.Id
@@ -106,32 +98,21 @@ func testOAuthStoreUpdateApp(t *testing.T, ss store.Store) {
// Lets update the app by removing the name // Lets update the app by removing the name
a1.Name = "" a1.Name = ""
if result := <-ss.OAuth().UpdateApp(&a1); result.Err == nil { _, err = ss.OAuth().UpdateApp(&a1)
t.Fatal("Should have failed. App name is not set") require.NotNil(t, err, "Should have failed. App name is not set")
}
// Lets not find the app that we are trying to update // Lets not find the app that we are trying to update
a1.Id = "fake0123456789abcderfgret1" a1.Id = "fake0123456789abcderfgret1"
a1.Name = "NewName" a1.Name = "NewName"
if result := <-ss.OAuth().UpdateApp(&a1); result.Err == nil { _, err = ss.OAuth().UpdateApp(&a1)
t.Fatal("Should have failed. Not able to find the app") require.NotNil(t, err, "Should have failed. Not able to find the app")
}
a1.Id = id a1.Id = id
if result := <-ss.OAuth().UpdateApp(&a1); result.Err != nil { ua, err := ss.OAuth().UpdateApp(&a1)
t.Fatal(result.Err) require.Nil(t, err)
} else { require.Equal(t, ua.Name, "NewName", "name did not update")
ua1 := (result.Data.([2]*model.OAuthApp)[0]) require.NotEqual(t, ua.CreateAt, 1, "create at should not have updated")
if ua1.Name != "NewName" { require.NotEqual(t, ua.CreatorId, "12345678901234567890123456", "creator id should not have updated")
t.Fatal("name did not update")
}
if ua1.CreateAt == 1 {
t.Fatal("create at should not have updated")
}
if ua1.CreatorId == "12345678901234567890123456" {
t.Fatal("creator id should not have updated")
}
}
} }
func testOAuthStoreSaveAccessData(t *testing.T, ss store.Store) { func testOAuthStoreSaveAccessData(t *testing.T, ss store.Store) {
@@ -140,17 +121,15 @@ func testOAuthStoreSaveAccessData(t *testing.T, ss store.Store) {
a1.UserId = model.NewId() a1.UserId = model.NewId()
// Lets try and save an incomplete access data // Lets try and save an incomplete access data
if err := (<-ss.OAuth().SaveAccessData(&a1)).Err; err == nil { _, err := ss.OAuth().SaveAccessData(&a1)
t.Fatal("Should have failed. Access data needs the token") require.NotNil(t, err, "Should have failed. Access data needs the token")
}
a1.Token = model.NewId() a1.Token = model.NewId()
a1.RefreshToken = model.NewId() a1.RefreshToken = model.NewId()
a1.RedirectUri = "http://example.com" a1.RedirectUri = "http://example.com"
if err := (<-ss.OAuth().SaveAccessData(&a1)).Err; err != nil { _, err = ss.OAuth().SaveAccessData(&a1)
t.Fatal(err) require.Nil(t, err)
}
} }
func testOAuthUpdateAccessData(t *testing.T, ss store.Store) { func testOAuthUpdateAccessData(t *testing.T, ss store.Store) {
@@ -161,32 +140,26 @@ func testOAuthUpdateAccessData(t *testing.T, ss store.Store) {
a1.RefreshToken = model.NewId() a1.RefreshToken = model.NewId()
a1.ExpiresAt = model.GetMillis() a1.ExpiresAt = model.GetMillis()
a1.RedirectUri = "http://example.com" a1.RedirectUri = "http://example.com"
store.Must(ss.OAuth().SaveAccessData(&a1)) _, err := ss.OAuth().SaveAccessData(&a1)
require.Nil(t, err)
//Try to update to invalid Refresh Token //Try to update to invalid Refresh Token
refreshToken := a1.RefreshToken refreshToken := a1.RefreshToken
a1.RefreshToken = model.NewId() + "123" a1.RefreshToken = model.NewId() + "123"
if err := (<-ss.OAuth().UpdateAccessData(&a1)).Err; err == nil { _, err = ss.OAuth().UpdateAccessData(&a1)
t.Fatal("Should have failed with invalid token") require.NotNil(t, err, "Should have failed with invalid token")
}
//Try to update to invalid RedirectUri //Try to update to invalid RedirectUri
a1.RefreshToken = model.NewId() a1.RefreshToken = model.NewId()
a1.RedirectUri = "" a1.RedirectUri = ""
if err := (<-ss.OAuth().UpdateAccessData(&a1)).Err; err == nil { _, err = ss.OAuth().UpdateAccessData(&a1)
t.Fatal("Should have failed with invalid Redirect URI") require.NotNil(t, err, "Should have failed with invalid Redirect URI")
}
// Should update fine // Should update fine
a1.RedirectUri = "http://example.com" a1.RedirectUri = "http://example.com"
if result := <-ss.OAuth().UpdateAccessData(&a1); result.Err != nil { ra1, err := ss.OAuth().UpdateAccessData(&a1)
t.Fatal(result.Err) require.Nil(t, err)
} else { require.NotEqual(t, ra1.RefreshToken, refreshToken, "refresh tokens didn't match")
ra1 := result.Data.(*model.AccessData)
if ra1.RefreshToken == refreshToken {
t.Fatal("refresh tokens didn't match")
}
}
} }
func testOAuthStoreGetAccessData(t *testing.T, ss store.Store) { func testOAuthStoreGetAccessData(t *testing.T, ss store.Store) {
@@ -197,43 +170,30 @@ func testOAuthStoreGetAccessData(t *testing.T, ss store.Store) {
a1.RefreshToken = model.NewId() a1.RefreshToken = model.NewId()
a1.ExpiresAt = model.GetMillis() a1.ExpiresAt = model.GetMillis()
a1.RedirectUri = "http://example.com" a1.RedirectUri = "http://example.com"
store.Must(ss.OAuth().SaveAccessData(&a1)) _, err := ss.OAuth().SaveAccessData(&a1)
require.Nil(t, err)
if err := (<-ss.OAuth().GetAccessData("invalidToken")).Err; err == nil { _, err = ss.OAuth().GetAccessData("invalidToken")
t.Fatal("Should have failed. There is no data with an invalid token") require.NotNil(t, err, "Should have failed. There is no data with an invalid token")
}
if result := <-ss.OAuth().GetAccessData(a1.Token); result.Err != nil { ra1, err := ss.OAuth().GetAccessData(a1.Token)
t.Fatal(result.Err) require.Nil(t, err)
} else { assert.Equal(t, a1.Token, ra1.Token, "tokens didn't match")
ra1 := result.Data.(*model.AccessData)
if a1.Token != ra1.Token {
t.Fatal("tokens didn't match")
}
}
if err := (<-ss.OAuth().GetPreviousAccessData(a1.UserId, a1.ClientId)).Err; err != nil { _, err = ss.OAuth().GetPreviousAccessData(a1.UserId, a1.ClientId)
t.Fatal(err) require.Nil(t, err)
}
if err := (<-ss.OAuth().GetPreviousAccessData("user", "junk")).Err; err != nil { _, err = ss.OAuth().GetPreviousAccessData("user", "junk")
t.Fatal(err) require.Nil(t, err)
}
// Try to get the Access data using an invalid refresh token // Try to get the Access data using an invalid refresh token
if err := (<-ss.OAuth().GetAccessDataByRefreshToken(a1.Token)).Err; err == nil { _, err = ss.OAuth().GetAccessDataByRefreshToken(a1.Token)
t.Fatal("Should have failed. There is no data with an invalid token") require.NotNil(t, err, "Should have failed. There is no data with an invalid token")
}
// Get the Access Data using the refresh token // Get the Access Data using the refresh token
if result := <-ss.OAuth().GetAccessDataByRefreshToken(a1.RefreshToken); result.Err != nil { ra1, err = ss.OAuth().GetAccessDataByRefreshToken(a1.RefreshToken)
t.Fatal(result.Err) require.Nil(t, err)
} else { assert.Equal(t, a1.RefreshToken, ra1.RefreshToken, "tokens didn't match")
ra1 := result.Data.(*model.AccessData)
if a1.RefreshToken != ra1.RefreshToken {
t.Fatal("tokens didn't match")
}
}
} }
func testOAuthStoreRemoveAccessData(t *testing.T, ss store.Store) { func testOAuthStoreRemoveAccessData(t *testing.T, ss store.Store) {
@@ -243,18 +203,14 @@ func testOAuthStoreRemoveAccessData(t *testing.T, ss store.Store) {
a1.Token = model.NewId() a1.Token = model.NewId()
a1.RefreshToken = model.NewId() a1.RefreshToken = model.NewId()
a1.RedirectUri = "http://example.com" a1.RedirectUri = "http://example.com"
store.Must(ss.OAuth().SaveAccessData(&a1)) _, err := ss.OAuth().SaveAccessData(&a1)
require.Nil(t, err)
if err := (<-ss.OAuth().RemoveAccessData(a1.Token)).Err; err != nil { err = ss.OAuth().RemoveAccessData(a1.Token)
t.Fatal(err) require.Nil(t, err)
}
if result := (<-ss.OAuth().GetPreviousAccessData(a1.UserId, a1.ClientId)); result.Err != nil { result, _ := ss.OAuth().GetPreviousAccessData(a1.UserId, a1.ClientId)
} else { require.Nil(t, result, "did not delete access token")
if result.Data != nil {
t.Fatal("did not delete access token")
}
}
} }
func testOAuthStoreSaveAuthData(t *testing.T, ss store.Store) { func testOAuthStoreSaveAuthData(t *testing.T, ss store.Store) {
@@ -263,9 +219,8 @@ func testOAuthStoreSaveAuthData(t *testing.T, ss store.Store) {
a1.UserId = model.NewId() a1.UserId = model.NewId()
a1.Code = model.NewId() a1.Code = model.NewId()
a1.RedirectUri = "http://example.com" a1.RedirectUri = "http://example.com"
if err := (<-ss.OAuth().SaveAuthData(&a1)).Err; err != nil { _, err := ss.OAuth().SaveAuthData(&a1)
t.Fatal(err) require.Nil(t, err)
}
} }
func testOAuthStoreGetAuthData(t *testing.T, ss store.Store) { func testOAuthStoreGetAuthData(t *testing.T, ss store.Store) {
@@ -274,11 +229,11 @@ func testOAuthStoreGetAuthData(t *testing.T, ss store.Store) {
a1.UserId = model.NewId() a1.UserId = model.NewId()
a1.Code = model.NewId() a1.Code = model.NewId()
a1.RedirectUri = "http://example.com" a1.RedirectUri = "http://example.com"
store.Must(ss.OAuth().SaveAuthData(&a1)) _, err := ss.OAuth().SaveAuthData(&a1)
require.Nil(t, err)
if err := (<-ss.OAuth().GetAuthData(a1.Code)).Err; err != nil { _, err = ss.OAuth().GetAuthData(a1.Code)
t.Fatal(err) require.Nil(t, err)
}
} }
func testOAuthStoreRemoveAuthData(t *testing.T, ss store.Store) { func testOAuthStoreRemoveAuthData(t *testing.T, ss store.Store) {
@@ -287,15 +242,14 @@ func testOAuthStoreRemoveAuthData(t *testing.T, ss store.Store) {
a1.UserId = model.NewId() a1.UserId = model.NewId()
a1.Code = model.NewId() a1.Code = model.NewId()
a1.RedirectUri = "http://example.com" a1.RedirectUri = "http://example.com"
store.Must(ss.OAuth().SaveAuthData(&a1)) _, err := ss.OAuth().SaveAuthData(&a1)
require.Nil(t, err)
if err := (<-ss.OAuth().RemoveAuthData(a1.Code)).Err; err != nil { err = ss.OAuth().RemoveAuthData(a1.Code)
t.Fatal(err) require.Nil(t, err)
}
if err := (<-ss.OAuth().GetAuthData(a1.Code)).Err; err == nil { _, err = ss.OAuth().GetAuthData(a1.Code)
t.Fatal("should have errored - auth code removed") require.NotNil(t, err, "should have errored - auth code removed")
}
} }
func testOAuthStoreRemoveAuthDataByUser(t *testing.T, ss store.Store) { func testOAuthStoreRemoveAuthDataByUser(t *testing.T, ss store.Store) {
@@ -304,11 +258,11 @@ func testOAuthStoreRemoveAuthDataByUser(t *testing.T, ss store.Store) {
a1.UserId = model.NewId() a1.UserId = model.NewId()
a1.Code = model.NewId() a1.Code = model.NewId()
a1.RedirectUri = "http://example.com" a1.RedirectUri = "http://example.com"
store.Must(ss.OAuth().SaveAuthData(&a1)) _, err := ss.OAuth().SaveAuthData(&a1)
require.Nil(t, err)
if err := (<-ss.OAuth().PermanentDeleteAuthDataByUser(a1.UserId)).Err; err != nil { err = ss.OAuth().PermanentDeleteAuthDataByUser(a1.UserId)
t.Fatal(err) require.Nil(t, err)
}
} }
func testOAuthGetAuthorizedApps(t *testing.T, ss store.Store) { func testOAuthGetAuthorizedApps(t *testing.T, ss store.Store) {
@@ -317,16 +271,13 @@ func testOAuthGetAuthorizedApps(t *testing.T, ss store.Store) {
a1.Name = "TestApp" + model.NewId() a1.Name = "TestApp" + model.NewId()
a1.CallbackUrls = []string{"https://nowhere.com"} a1.CallbackUrls = []string{"https://nowhere.com"}
a1.Homepage = "https://nowhere.com" a1.Homepage = "https://nowhere.com"
store.Must(ss.OAuth().SaveApp(&a1)) _, err := ss.OAuth().SaveApp(&a1)
require.Nil(t, err)
// Lets try and get an Authorized app for a user who hasn't authorized it // Lets try and get an Authorized app for a user who hasn't authorized it
if result := <-ss.OAuth().GetAuthorizedApps("fake0123456789abcderfgret1", 0, 1000); result.Err == nil { apps, err := ss.OAuth().GetAuthorizedApps("fake0123456789abcderfgret1", 0, 1000)
if len(result.Data.([]*model.OAuthApp)) > 0 { require.Nil(t, err)
t.Fatal("Should have failed. Fake user hasn't authorized the app") assert.Len(t, apps, 0, "Should have failed. Fake user hasn't authorized the app")
}
} else {
t.Fatal(result.Err)
}
// allow the app // allow the app
p := model.Preference{} p := model.Preference{}
@@ -334,17 +285,12 @@ func testOAuthGetAuthorizedApps(t *testing.T, ss store.Store) {
p.Category = model.PREFERENCE_CATEGORY_AUTHORIZED_OAUTH_APP p.Category = model.PREFERENCE_CATEGORY_AUTHORIZED_OAUTH_APP
p.Name = a1.Id p.Name = a1.Id
p.Value = "true" p.Value = "true"
err := ss.Preference().Save(&model.Preferences{p}) err = ss.Preference().Save(&model.Preferences{p})
require.Nil(t, err) require.Nil(t, err)
if result := <-ss.OAuth().GetAuthorizedApps(a1.CreatorId, 0, 1000); result.Err != nil { apps, err = ss.OAuth().GetAuthorizedApps(a1.CreatorId, 0, 1000)
t.Fatal(result.Err) require.Nil(t, err)
} else { assert.NotEqual(t, len(apps), 0, "It should have return apps")
apps := result.Data.([]*model.OAuthApp)
if len(apps) == 0 {
t.Fatal("It should have return apps")
}
}
} }
func testOAuthGetAccessDataByUserForApp(t *testing.T, ss store.Store) { func testOAuthGetAccessDataByUserForApp(t *testing.T, ss store.Store) {
@@ -353,7 +299,8 @@ func testOAuthGetAccessDataByUserForApp(t *testing.T, ss store.Store) {
a1.Name = "TestApp" + model.NewId() a1.Name = "TestApp" + model.NewId()
a1.CallbackUrls = []string{"https://nowhere.com"} a1.CallbackUrls = []string{"https://nowhere.com"}
a1.Homepage = "https://nowhere.com" a1.Homepage = "https://nowhere.com"
store.Must(ss.OAuth().SaveApp(&a1)) _, err := ss.OAuth().SaveApp(&a1)
require.Nil(t, err)
// allow the app // allow the app
p := model.Preference{} p := model.Preference{}
@@ -361,17 +308,12 @@ func testOAuthGetAccessDataByUserForApp(t *testing.T, ss store.Store) {
p.Category = model.PREFERENCE_CATEGORY_AUTHORIZED_OAUTH_APP p.Category = model.PREFERENCE_CATEGORY_AUTHORIZED_OAUTH_APP
p.Name = a1.Id p.Name = a1.Id
p.Value = "true" p.Value = "true"
err := ss.Preference().Save(&model.Preferences{p}) err = ss.Preference().Save(&model.Preferences{p})
require.Nil(t, err) require.Nil(t, err)
if result := <-ss.OAuth().GetAuthorizedApps(a1.CreatorId, 0, 1000); result.Err != nil { apps, err := ss.OAuth().GetAuthorizedApps(a1.CreatorId, 0, 1000)
t.Fatal(result.Err) require.Nil(t, err)
} else { assert.NotEqual(t, len(apps), 0, "It should have return apps")
apps := result.Data.([]*model.OAuthApp)
if len(apps) == 0 {
t.Fatal("It should have return apps")
}
}
// save the token // save the token
ad1 := model.AccessData{} ad1 := model.AccessData{}
@@ -381,18 +323,12 @@ func testOAuthGetAccessDataByUserForApp(t *testing.T, ss store.Store) {
ad1.RefreshToken = model.NewId() ad1.RefreshToken = model.NewId()
ad1.RedirectUri = "http://example.com" ad1.RedirectUri = "http://example.com"
if err := (<-ss.OAuth().SaveAccessData(&ad1)).Err; err != nil { _, err = ss.OAuth().SaveAccessData(&ad1)
t.Fatal(err) require.Nil(t, err)
}
if result := <-ss.OAuth().GetAccessDataByUserForApp(a1.CreatorId, a1.Id); result.Err != nil { accessData, err := ss.OAuth().GetAccessDataByUserForApp(a1.CreatorId, a1.Id)
t.Fatal(result.Err) require.Nil(t, err)
} else { assert.NotEqual(t, len(accessData), 0, "It should have return access data")
accessData := result.Data.([]*model.AccessData)
if len(accessData) == 0 {
t.Fatal("It should have return access data")
}
}
} }
func testOAuthStoreDeleteApp(t *testing.T, ss store.Store) { func testOAuthStoreDeleteApp(t *testing.T, ss store.Store) {
@@ -401,19 +337,19 @@ func testOAuthStoreDeleteApp(t *testing.T, ss store.Store) {
a1.Name = "TestApp" + model.NewId() a1.Name = "TestApp" + model.NewId()
a1.CallbackUrls = []string{"https://nowhere.com"} a1.CallbackUrls = []string{"https://nowhere.com"}
a1.Homepage = "https://nowhere.com" a1.Homepage = "https://nowhere.com"
store.Must(ss.OAuth().SaveApp(&a1)) _, err := ss.OAuth().SaveApp(&a1)
require.Nil(t, err)
// delete a non-existent app // delete a non-existent app
if err := (<-ss.OAuth().DeleteApp("fakeclientId")).Err; err != nil { err = ss.OAuth().DeleteApp("fakeclientId")
t.Fatal(err) require.Nil(t, err)
}
s1 := &model.Session{} s1 := &model.Session{}
s1.UserId = model.NewId() s1.UserId = model.NewId()
s1.Token = model.NewId() s1.Token = model.NewId()
s1.IsOAuth = true s1.IsOAuth = true
s1, err := ss.Session().Save(s1) s1, err = ss.Session().Save(s1)
require.Nil(t, err) require.Nil(t, err)
ad1 := model.AccessData{} ad1 := model.AccessData{}
@@ -423,17 +359,16 @@ func testOAuthStoreDeleteApp(t *testing.T, ss store.Store) {
ad1.RefreshToken = model.NewId() ad1.RefreshToken = model.NewId()
ad1.RedirectUri = "http://example.com" ad1.RedirectUri = "http://example.com"
store.Must(ss.OAuth().SaveAccessData(&ad1)) _, err = ss.OAuth().SaveAccessData(&ad1)
require.Nil(t, err)
if err := (<-ss.OAuth().DeleteApp(a1.Id)).Err; err != nil { err = ss.OAuth().DeleteApp(a1.Id)
t.Fatal(err) require.Nil(t, err)
}
if _, err := ss.Session().Get(s1.Token); err == nil { if _, err = ss.Session().Get(s1.Token); err == nil {
t.Fatal("should error - session should be deleted") t.Fatal("should error - session should be deleted")
} }
if err := (<-ss.OAuth().GetAccessData(s1.Token)).Err; err == nil { _, err = ss.OAuth().GetAccessData(s1.Token)
t.Fatal("should error - access data should be deleted") require.NotNil(t, err, "should error - access data should be deleted")
}
} }