diff --git a/api4/oauth_test.go b/api4/oauth_test.go index 7c5df9fb95..f7d203e1f0 100644 --- a/api4/oauth_test.go +++ b/api4/oauth_test.go @@ -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} - <-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("client_id", oauthApp.Id) diff --git a/app/oauth.go b/app/oauth.go index 322060281c..364b4b0b3b 100644 --- a/app/oauth.go +++ b/app/oauth.go @@ -34,24 +34,14 @@ func (a *App) CreateOAuthApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppEr app.ClientSecret = model.NewId() - result := <-a.Srv.Store.OAuth().SaveApp(app) - if result.Err != nil { - return nil, result.Err - } - - return result.Data.(*model.OAuthApp), nil + return a.Srv.Store.OAuth().SaveApp(app) } func (a *App) GetOAuthApp(appId string) (*model.OAuthApp, *model.AppError) { if !*a.Config().ServiceSettings.EnableOAuthServiceProvider { return nil, model.NewAppError("GetOAuthApp", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented) } - result := <-a.Srv.Store.OAuth().GetApp(appId) - if result.Err != nil { - return nil, result.Err - } - - return result.Data.(*model.OAuthApp), nil + return a.Srv.Store.OAuth().GetApp(appId) } 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.ClientSecret = oldApp.ClientSecret - result := <-a.Srv.Store.OAuth().UpdateApp(updatedApp) - if result.Err != nil { - return nil, result.Err - } - - return result.Data.([2]*model.OAuthApp)[0], nil + return a.Srv.Store.OAuth().UpdateApp(updatedApp) } 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) } - if err := (<-a.Srv.Store.OAuth().DeleteApp(appId)).Err; err != nil { + if err := a.Srv.Store.OAuth().DeleteApp(appId); err != nil { 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) } - result := <-a.Srv.Store.OAuth().GetApps(page*perPage, perPage) - if result.Err != nil { - return nil, result.Err - } - - return result.Data.([]*model.OAuthApp), nil + return a.Srv.Store.OAuth().GetApps(page*perPage, perPage) } 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) } - result := <-a.Srv.Store.OAuth().GetAppByUser(userId, page*perPage, perPage) - if result.Err != nil { - return nil, result.Err - } - - return result.Data.([]*model.OAuthApp), nil + return a.Srv.Store.OAuth().GetAppByUser(userId, page*perPage, perPage) } 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.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 } @@ -150,18 +125,16 @@ func (a *App) AllowOAuthAppAccessToUser(userId string, authRequest *model.Author authRequest.Scope = model.DEFAULT_SCOPE } - result := <-a.Srv.Store.OAuth().GetApp(authRequest.ClientId) - if result.Err != nil { - return "", result.Err + oauthApp, err := a.Srv.Store.OAuth().GetApp(authRequest.ClientId) + if err != nil { + return "", err } - oauthApp := result.Data.(*model.OAuthApp) if !oauthApp.IsValidRedirectURL(authRequest.RedirectUri) { return "", model.NewAppError("AllowOAuthAppAccessToUser", "api.oauth.allow_oauth.redirect_callback.app_error", nil, "", http.StatusBadRequest) } var redirectURI string - var err *model.AppError switch authRequest.ResponseType { 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} - if result := <-a.Srv.Store.OAuth().SaveAccessData(accessData); result.Err != nil { - mlog.Error(fmt.Sprint(result.Err)) + if _, err := a.Srv.Store.OAuth().SaveAccessData(accessData); err != nil { + mlog.Error(fmt.Sprint(err)) 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) } - result := <-a.Srv.Store.OAuth().GetApp(clientId) - if result.Err != nil { + oauthApp, err := a.Srv.Store.OAuth().GetApp(clientId) + if err != nil { 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 { 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 if grantType == model.ACCESS_TOKEN_GRANT_TYPE { var authData *model.AuthData - result := <-a.Srv.Store.OAuth().GetAuthData(code) - if result.Err != nil { + authData, err = a.Srv.Store.OAuth().GetAuthData(code) + if err != nil { 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() { - <-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) } @@ -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) } - var err *model.AppError user, err = a.Srv.Store.User().Get(authData.UserId) if err != nil { 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) - if result.Err != nil { + accessData, err = a.Srv.Store.OAuth().GetPreviousAccessData(user.Id, clientId) + if err != nil { return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal.app_error", nil, "", http.StatusBadRequest) } - if result.Data != nil { - accessData := result.Data.(*model.AccessData) + if accessData != nil { 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 { return nil, err } @@ -287,16 +257,17 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c } } } else { + var session *model.Session // 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 { 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} - if result := <-a.Srv.Store.OAuth().SaveAccessData(accessData); result.Err != nil { - mlog.Error(fmt.Sprint(result.Err)) + if _, err = a.Srv.Store.OAuth().SaveAccessData(accessData); err != nil { + mlog.Error(fmt.Sprint(err)) 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 { // When grantType is refresh_token - result := <-a.Srv.Store.OAuth().GetAccessDataByRefreshToken(refreshToken) - if result.Err != nil { + accessData, err = a.Srv.Store.OAuth().GetAccessDataByRefreshToken(refreshToken) + if err != nil { 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) if err != nil { @@ -366,8 +336,8 @@ func (a *App) newSessionUpdateToken(appName string, accessData *model.AccessData accessData.RefreshToken = model.NewId() accessData.ExpiresAt = session.ExpiresAt - if result := <-a.Srv.Store.OAuth().UpdateAccessData(accessData); result.Err != nil { - mlog.Error(fmt.Sprint(result.Err)) + if _, err := a.Srv.Store.OAuth().UpdateAccessData(accessData); err != nil { + mlog.Error(fmt.Sprint(err)) return nil, model.NewAppError("newSessionUpdateToken", "web.get_access_token.internal_saving.app_error", nil, "", http.StatusInternalServerError) } 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) } - result := <-a.Srv.Store.OAuth().GetAuthorizedApps(userId, page*perPage, perPage) - if result.Err != nil { - return nil, result.Err + apps, err := a.Srv.Store.OAuth().GetAuthorizedApps(userId, page*perPage, perPage) + if err != nil { + return nil, err } - apps := result.Data.([]*model.OAuthApp) for k, a := range apps { a.Sanitize() @@ -439,19 +408,18 @@ func (a *App) DeauthorizeOAuthAppForUser(userId, appId string) *model.AppError { } // Revoke app sessions - result := <-a.Srv.Store.OAuth().GetAccessDataByUserForApp(userId, appId) - if result.Err != nil { - return result.Err + accessData, err := a.Srv.Store.OAuth().GetAccessDataByUserForApp(userId, appId) + if err != nil { + return err } - accessData := result.Data.([]*model.AccessData) for _, ad := range accessData { if err := a.RevokeAccessToken(ad.Token); err != nil { return err } - if rad := <-a.Srv.Store.OAuth().RemoveAccessData(ad.Token); rad.Err != nil { - return rad.Err + if err := a.Srv.Store.OAuth().RemoveAccessData(ad.Token); err != nil { + return err } } @@ -469,8 +437,8 @@ func (a *App) RegenerateOAuthAppSecret(app *model.OAuthApp) (*model.OAuthApp, *m } app.ClientSecret = model.NewId() - if update := <-a.Srv.Store.OAuth().UpdateApp(app); update.Err != nil { - return nil, update.Err + if _, err := a.Srv.Store.OAuth().UpdateApp(app); err != nil { + return nil, err } return app, nil @@ -485,11 +453,11 @@ func (a *App) RevokeAccessToken(token string) *model.AppError { 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) } - 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) } diff --git a/app/oauth_test.go b/app/oauth_test.go index ffeb0dd10a..08d164ba29 100644 --- a/app/oauth_test.go +++ b/app/oauth_test.go @@ -92,11 +92,10 @@ func TestOAuthRevokeAccessToken(t *testing.T) { accessData.ClientId = model.NewId() accessData.ExpiresAt = session.ExpiresAt - if result := <-th.App.Srv.Store.OAuth().SaveAccessData(accessData); result.Err != nil { - t.Fatal(result.Err) - } + _, err := th.App.Srv.Store.OAuth().SaveAccessData(accessData) + 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) } } @@ -136,15 +135,14 @@ func TestOAuthDeleteApp(t *testing.T) { accessData.ClientId = a1.Id accessData.ExpiresAt = session.ExpiresAt - if result := <-th.App.Srv.Store.OAuth().SaveAccessData(accessData); result.Err != nil { - t.Fatal(result.Err) - } + _, err = th.App.Srv.Store.OAuth().SaveAccessData(accessData) + 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) } - 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") } } diff --git a/app/user.go b/app/user.go index 1c2ccb3bfb..2004b813f0 100644 --- a/app/user.go +++ b/app/user.go @@ -1436,8 +1436,8 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError { return result.Err } - if result := <-a.Srv.Store.OAuth().PermanentDeleteAuthDataByUser(user.Id); result.Err != nil { - return result.Err + if err := a.Srv.Store.OAuth().PermanentDeleteAuthDataByUser(user.Id); err != nil { + return err } if err := a.Srv.Store.Webhook().PermanentDeleteIncomingByUser(user.Id); err != nil { diff --git a/store/sqlstore/oauth_store.go b/store/sqlstore/oauth_store.go index dd8e4fc5ba..1458b618ae 100644 --- a/store/sqlstore/oauth_store.go +++ b/store/sqlstore/oauth_store.go @@ -59,277 +59,234 @@ func (as SqlOAuthStore) CreateIndexesIfNotExists() { as.CreateIndexIfNotExists("idx_oauthauthdata_client_id", "OAuthAuthData", "Code") } -func (as SqlOAuthStore) SaveApp(app *model.OAuthApp) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - 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 - } +func (as SqlOAuthStore) SaveApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError) { + if len(app.Id) > 0 { + return nil, model.NewAppError("SqlOAuthStore.SaveApp", "store.sql_oauth.save_app.existing.app_error", nil, "app_id="+app.Id, http.StatusBadRequest) + } - app.PreSave() - if result.Err = app.IsValid(); result.Err != nil { - return - } + app.PreSave() + if err := app.IsValid(); err != nil { + return nil, err + } - 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) - } else { - result.Data = app - } - }) + if err := as.GetMaster().Insert(app); err != nil { + return nil, model.NewAppError("SqlOAuthStore.SaveApp", "store.sql_oauth.save_app.save.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError) + } + return app, nil } -func (as SqlOAuthStore) UpdateApp(app *model.OAuthApp) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - app.PreUpdate() +func (as SqlOAuthStore) UpdateApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError) { + app.PreUpdate() - if result.Err = app.IsValid(); result.Err != nil { - return - } + if err := app.IsValid(); err != nil { + return nil, err + } - 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) - app.CreateAt = oldApp.CreateAt - app.CreatorId = oldApp.CreatorId + 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 count, err := as.GetMaster().Update(app); err != nil { - result.Err = model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.updating.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError) - } else if count != 1 { - 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} - } - } - }) + oldApp := oldAppResult.(*model.OAuthApp) + app.CreateAt = oldApp.CreateAt + app.CreatorId = oldApp.CreatorId + + count, err := as.GetMaster().Update(app) + if err != nil { + return nil, model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.updating.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError) + } + 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 { - return store.Do(func(result *store.StoreResult) { - if obj, err := as.GetReplica().Get(model.OAuthApp{}, id); err != nil { - result.Err = 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) - } - }) +func (as SqlOAuthStore) GetApp(id string) (*model.OAuthApp, *model.AppError) { + obj, err := as.GetReplica().Get(model.OAuthApp{}, id) + if err != nil { + return nil, model.NewAppError("SqlOAuthStore.GetApp", "store.sql_oauth.get_app.finding.app_error", nil, "app_id="+id+", "+err.Error(), http.StatusInternalServerError) + } + 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 { - return store.Do(func(result *store.StoreResult) { - var apps []*model.OAuthApp +func (as SqlOAuthStore) GetAppByUser(userId string, offset, limit int) ([]*model.OAuthApp, *model.AppError) { + 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 { - result.Err = model.NewAppError("SqlOAuthStore.GetAppByUser", "store.sql_oauth.get_app_by_user.find.app_error", nil, "user_id="+userId+", "+err.Error(), http.StatusInternalServerError) - } + 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 { + 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 { - return store.Do(func(result *store.StoreResult) { - var apps []*model.OAuthApp +func (as SqlOAuthStore) GetApps(offset, limit int) ([]*model.OAuthApp, *model.AppError) { + 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 { - result.Err = model.NewAppError("SqlOAuthStore.GetAppByUser", "store.sql_oauth.get_apps.find.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) - } + if _, err := as.GetReplica().Select(&apps, "SELECT * FROM OAuthApps LIMIT :Limit OFFSET :Offset", map[string]interface{}{"Offset": offset, "Limit": limit}); err != nil { + 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 { - return store.Do(func(result *store.StoreResult) { - var apps []*model.OAuthApp +func (as SqlOAuthStore) GetAuthorizedApps(userId string, offset, limit int) ([]*model.OAuthApp, *model.AppError) { + var apps []*model.OAuthApp - if _, err := as.GetReplica().Select(&apps, - `SELECT o.* FROM OAuthApps AS o INNER JOIN + if _, err := as.GetReplica().Select(&apps, + `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 { - 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 { - return store.Do(func(result *store.StoreResult) { - // wrap in a transaction so that if one fails, everything fails - transaction, err := as.GetMaster().Begin() - if err != nil { - result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) - } else { - defer finalizeTransaction(transaction) - if extrasResult := as.deleteApp(transaction, id); extrasResult.Err != nil { - *result = extrasResult - } +func (as SqlOAuthStore) DeleteApp(id string) *model.AppError { + // wrap in a transaction so that if one fails, everything fails + transaction, err := as.GetMaster().Begin() + if err != nil { + return model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) + } + defer finalizeTransaction(transaction) - if result.Err == nil { - if err := transaction.Commit(); err != nil { - // 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) - } - } - } - }) + if err := as.deleteApp(transaction, id); err != nil { + return err + } + + if err := transaction.Commit(); err != nil { + // don't need to rollback here since the transaction is already closed + 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 { - return store.Do(func(result *store.StoreResult) { - if result.Err = accessData.IsValid(); result.Err != nil { - return - } +func (as SqlOAuthStore) SaveAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError) { + if err := accessData.IsValid(); err != nil { + return nil, err + } - 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) - } else { - result.Data = accessData - } - }) + if err := as.GetMaster().Insert(accessData); err != nil { + return nil, model.NewAppError("SqlOAuthStore.SaveAccessData", "store.sql_oauth.save_access_data.app_error", nil, err.Error(), http.StatusInternalServerError) + } + return accessData, nil } -func (as SqlOAuthStore) GetAccessData(token string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - accessData := model.AccessData{} +func (as SqlOAuthStore) GetAccessData(token string) (*model.AccessData, *model.AppError) { + accessData := model.AccessData{} - 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) - } else { - result.Data = &accessData - } - }) + if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil { + return nil, model.NewAppError("SqlOAuthStore.GetAccessData", "store.sql_oauth.get_access_data.app_error", nil, err.Error(), http.StatusInternalServerError) + } + return &accessData, nil } -func (as SqlOAuthStore) GetAccessDataByUserForApp(userId, clientId string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - var accessData []*model.AccessData +func (as SqlOAuthStore) GetAccessDataByUserForApp(userId, clientId string) ([]*model.AccessData, *model.AppError) { + var accessData []*model.AccessData - if _, err := as.GetReplica().Select(&accessData, - "SELECT * FROM OAuthAccessData WHERE UserId = :UserId AND ClientId = :ClientId", - 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) - } else { - result.Data = accessData - } - }) + if _, err := as.GetReplica().Select(&accessData, + "SELECT * FROM OAuthAccessData WHERE UserId = :UserId AND ClientId = :ClientId", + map[string]interface{}{"UserId": userId, "ClientId": clientId}); err != nil { + 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) + } + return accessData, nil } -func (as SqlOAuthStore) GetAccessDataByRefreshToken(token string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - accessData := model.AccessData{} +func (as SqlOAuthStore) GetAccessDataByRefreshToken(token string) (*model.AccessData, *model.AppError) { + accessData := model.AccessData{} - 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) - } else { - result.Data = &accessData - } - }) + if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE RefreshToken = :Token", map[string]interface{}{"Token": token}); err != nil { + return nil, model.NewAppError("SqlOAuthStore.GetAccessData", "store.sql_oauth.get_access_data.app_error", nil, err.Error(), http.StatusInternalServerError) + } + return &accessData, nil } -func (as SqlOAuthStore) GetPreviousAccessData(userId, clientId string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - accessData := model.AccessData{} +func (as SqlOAuthStore) GetPreviousAccessData(userId, clientId string) (*model.AccessData, *model.AppError) { + accessData := model.AccessData{} - if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE ClientId = :ClientId AND UserId = :UserId", - map[string]interface{}{"ClientId": clientId, "UserId": userId}); err != nil { - if strings.Contains(err.Error(), "no rows") { - result.Data = nil - } else { - result.Err = model.NewAppError("SqlOAuthStore.GetPreviousAccessData", "store.sql_oauth.get_previous_access_data.app_error", nil, err.Error(), http.StatusNotFound) - } - } else { - result.Data = &accessData + if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE ClientId = :ClientId AND UserId = :UserId", + map[string]interface{}{"ClientId": clientId, "UserId": userId}); err != nil { + if strings.Contains(err.Error(), "no rows") { + return nil, nil } - }) + return nil, model.NewAppError("SqlOAuthStore.GetPreviousAccessData", "store.sql_oauth.get_previous_access_data.app_error", nil, err.Error(), http.StatusNotFound) + } + return &accessData, nil } -func (as SqlOAuthStore) UpdateAccessData(accessData *model.AccessData) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - if result.Err = accessData.IsValid(); result.Err != nil { - return - } +func (as SqlOAuthStore) UpdateAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError) { + if err := accessData.IsValid(); err != nil { + return nil, err + } - 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 { - result.Err = model.NewAppError("SqlOAuthStore.Update", "store.sql_oauth.update_access_data.app_error", nil, - "clientId="+accessData.ClientId+",userId="+accessData.UserId+", "+err.Error(), http.StatusInternalServerError) - } else { - result.Data = accessData - } - }) + 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 { + 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) + } + return accessData, nil } -func (as SqlOAuthStore) RemoveAccessData(token string) store.StoreChannel { - 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 { - result.Err = model.NewAppError("SqlOAuthStore.RemoveAccessData", "store.sql_oauth.remove_access_data.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) - } - }) +func (as SqlOAuthStore) RemoveAccessData(token string) *model.AppError { + if _, err := as.GetMaster().Exec("DELETE FROM OAuthAccessData WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil { + 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 { - return store.Do(func(result *store.StoreResult) { - authData.PreSave() - if result.Err = authData.IsValid(); result.Err != nil { - return - } +func (as SqlOAuthStore) SaveAuthData(authData *model.AuthData) (*model.AuthData, *model.AppError) { + authData.PreSave() + if err := authData.IsValid(); err != nil { + return nil, err + } - 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) - } else { - result.Data = authData - } - }) + if err := as.GetMaster().Insert(authData); err != nil { + return nil, model.NewAppError("SqlOAuthStore.SaveAuthData", "store.sql_oauth.save_auth_data.app_error", nil, err.Error(), http.StatusInternalServerError) + } + return authData, nil } -func (as SqlOAuthStore) GetAuthData(code string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - if obj, err := as.GetReplica().Get(model.AuthData{}, code); err != nil { - result.Err = 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) - } - }) +func (as SqlOAuthStore) GetAuthData(code string) (*model.AuthData, *model.AppError) { + obj, err := as.GetReplica().Get(model.AuthData{}, code) + if err != nil { + return nil, model.NewAppError("SqlOAuthStore.GetAuthData", "store.sql_oauth.get_auth_data.finding.app_error", nil, err.Error(), http.StatusInternalServerError) + } + 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 { - return store.Do(func(result *store.StoreResult) { - _, err := as.GetMaster().Exec("DELETE FROM OAuthAuthData WHERE Code = :Code", map[string]interface{}{"Code": code}) - if err != nil { - result.Err = model.NewAppError("SqlOAuthStore.RemoveAuthData", "store.sql_oauth.remove_auth_data.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) - } - }) +func (as SqlOAuthStore) RemoveAuthData(code string) *model.AppError { + _, err := as.GetMaster().Exec("DELETE FROM OAuthAuthData WHERE Code = :Code", map[string]interface{}{"Code": code}) + if err != nil { + 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 { - return store.Do(func(result *store.StoreResult) { - _, err := as.GetMaster().Exec("DELETE FROM OAuthAccessData WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}) - 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) - } - }) +func (as SqlOAuthStore) PermanentDeleteAuthDataByUser(userId string) *model.AppError { + _, err := as.GetMaster().Exec("DELETE FROM OAuthAccessData WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}) + if err != nil { + 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 { - result := store.StoreResult{} - +func (as SqlOAuthStore) deleteApp(transaction *gorp.Transaction, clientId string) *model.AppError { 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 result + return model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError) } return as.deleteOAuthAppSessions(transaction, clientId) } -func (as SqlOAuthStore) deleteOAuthAppSessions(transaction *gorp.Transaction, clientId string) store.StoreResult { - result := store.StoreResult{} +func (as SqlOAuthStore) deleteOAuthAppSessions(transaction *gorp.Transaction, clientId string) *model.AppError { query := "" 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 { - result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError) - return result + return model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError) } return as.deleteOAuthTokens(transaction, clientId) } -func (as SqlOAuthStore) deleteOAuthTokens(transaction *gorp.Transaction, clientId string) store.StoreResult { - result := store.StoreResult{} - +func (as SqlOAuthStore) deleteOAuthTokens(transaction *gorp.Transaction, clientId string) *model.AppError { 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 result + return model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError) } return as.deleteAppExtras(transaction, clientId) } -func (as SqlOAuthStore) deleteAppExtras(transaction *gorp.Transaction, clientId string) store.StoreResult { - result := store.StoreResult{} - +func (as SqlOAuthStore) deleteAppExtras(transaction *gorp.Transaction, clientId string) *model.AppError { if _, err := transaction.Exec( `DELETE FROM Preferences WHERE Category = :Category 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 result + return model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_preference.delete.app_error", nil, err.Error(), http.StatusInternalServerError) } - return result + return nil } diff --git a/store/store.go b/store/store.go index 2fb0d11768..58ee08517d 100644 --- a/store/store.go +++ b/store/store.go @@ -352,24 +352,24 @@ type ComplianceStore interface { } type OAuthStore interface { - SaveApp(app *model.OAuthApp) StoreChannel - UpdateApp(app *model.OAuthApp) StoreChannel - GetApp(id string) StoreChannel - GetAppByUser(userId string, offset, limit int) StoreChannel - GetApps(offset, limit int) StoreChannel - GetAuthorizedApps(userId string, offset, limit int) StoreChannel - DeleteApp(id string) StoreChannel - SaveAuthData(authData *model.AuthData) StoreChannel - GetAuthData(code string) StoreChannel - RemoveAuthData(code string) StoreChannel - PermanentDeleteAuthDataByUser(userId string) StoreChannel - SaveAccessData(accessData *model.AccessData) StoreChannel - UpdateAccessData(accessData *model.AccessData) StoreChannel - GetAccessData(token string) StoreChannel - GetAccessDataByUserForApp(userId, clientId string) StoreChannel - GetAccessDataByRefreshToken(token string) StoreChannel - GetPreviousAccessData(userId, clientId string) StoreChannel - RemoveAccessData(token string) StoreChannel + SaveApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError) + UpdateApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError) + GetApp(id string) (*model.OAuthApp, *model.AppError) + GetAppByUser(userId string, offset, limit int) ([]*model.OAuthApp, *model.AppError) + GetApps(offset, limit int) ([]*model.OAuthApp, *model.AppError) + GetAuthorizedApps(userId string, offset, limit int) ([]*model.OAuthApp, *model.AppError) + DeleteApp(id string) *model.AppError + SaveAuthData(authData *model.AuthData) (*model.AuthData, *model.AppError) + GetAuthData(code string) (*model.AuthData, *model.AppError) + RemoveAuthData(code string) *model.AppError + PermanentDeleteAuthDataByUser(userId string) *model.AppError + SaveAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError) + UpdateAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError) + GetAccessData(token string) (*model.AccessData, *model.AppError) + GetAccessDataByUserForApp(userId, clientId string) ([]*model.AccessData, *model.AppError) + GetAccessDataByRefreshToken(token string) (*model.AccessData, *model.AppError) + GetPreviousAccessData(userId, clientId string) (*model.AccessData, *model.AppError) + RemoveAccessData(token string) *model.AppError } type SystemStore interface { diff --git a/store/storetest/mocks/OAuthStore.go b/store/storetest/mocks/OAuthStore.go index a39570b6ce..aabb877bce 100644 --- a/store/storetest/mocks/OAuthStore.go +++ b/store/storetest/mocks/OAuthStore.go @@ -6,7 +6,6 @@ package mocks import mock "github.com/stretchr/testify/mock" 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 type OAuthStore struct { @@ -14,15 +13,15 @@ type OAuthStore struct { } // 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) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { + var r0 *model.AppError + if rf, ok := ret.Get(0).(func(string) *model.AppError); ok { r0 = rf(id) } else { 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 -func (_m *OAuthStore) GetAccessData(token string) store.StoreChannel { +func (_m *OAuthStore) GetAccessData(token string) (*model.AccessData, *model.AppError) { ret := _m.Called(token) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { + var r0 *model.AccessData + if rf, ok := ret.Get(0).(func(string) *model.AccessData); ok { r0 = rf(token) } else { 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 -func (_m *OAuthStore) GetAccessDataByRefreshToken(token string) store.StoreChannel { +func (_m *OAuthStore) GetAccessDataByRefreshToken(token string) (*model.AccessData, *model.AppError) { ret := _m.Called(token) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { + var r0 *model.AccessData + if rf, ok := ret.Get(0).(func(string) *model.AccessData); ok { r0 = rf(token) } else { 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 -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) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok { + var r0 []*model.AccessData + if rf, ok := ret.Get(0).(func(string, string) []*model.AccessData); ok { r0 = rf(userId, clientId) } else { 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 -func (_m *OAuthStore) GetApp(id string) store.StoreChannel { +func (_m *OAuthStore) GetApp(id string) (*model.OAuthApp, *model.AppError) { ret := _m.Called(id) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { + var r0 *model.OAuthApp + if rf, ok := ret.Get(0).(func(string) *model.OAuthApp); ok { r0 = rf(id) } else { 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 -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) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, int, int) store.StoreChannel); ok { + var r0 []*model.OAuthApp + if rf, ok := ret.Get(0).(func(string, int, int) []*model.OAuthApp); ok { r0 = rf(userId, offset, limit) } else { 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 -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) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(int, int) store.StoreChannel); ok { + var r0 []*model.OAuthApp + if rf, ok := ret.Get(0).(func(int, int) []*model.OAuthApp); ok { r0 = rf(offset, limit) } else { 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 -func (_m *OAuthStore) GetAuthData(code string) store.StoreChannel { +func (_m *OAuthStore) GetAuthData(code string) (*model.AuthData, *model.AppError) { ret := _m.Called(code) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { + var r0 *model.AuthData + if rf, ok := ret.Get(0).(func(string) *model.AuthData); ok { r0 = rf(code) } else { 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 -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) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, int, int) store.StoreChannel); ok { + var r0 []*model.OAuthApp + if rf, ok := ret.Get(0).(func(string, int, int) []*model.OAuthApp); ok { r0 = rf(userId, offset, limit) } else { 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 -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) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok { + var r0 *model.AccessData + if rf, ok := ret.Get(0).(func(string, string) *model.AccessData); ok { r0 = rf(userId, clientId) } else { 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 -func (_m *OAuthStore) PermanentDeleteAuthDataByUser(userId string) store.StoreChannel { +func (_m *OAuthStore) PermanentDeleteAuthDataByUser(userId string) *model.AppError { ret := _m.Called(userId) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { + var r0 *model.AppError + if rf, ok := ret.Get(0).(func(string) *model.AppError); ok { r0 = rf(userId) } else { 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 -func (_m *OAuthStore) RemoveAccessData(token string) store.StoreChannel { +func (_m *OAuthStore) RemoveAccessData(token string) *model.AppError { ret := _m.Called(token) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { + var r0 *model.AppError + if rf, ok := ret.Get(0).(func(string) *model.AppError); ok { r0 = rf(token) } else { 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 -func (_m *OAuthStore) RemoveAuthData(code string) store.StoreChannel { +func (_m *OAuthStore) RemoveAuthData(code string) *model.AppError { ret := _m.Called(code) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { + var r0 *model.AppError + if rf, ok := ret.Get(0).(func(string) *model.AppError); ok { r0 = rf(code) } else { 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 -func (_m *OAuthStore) SaveAccessData(accessData *model.AccessData) store.StoreChannel { +func (_m *OAuthStore) SaveAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError) { ret := _m.Called(accessData) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(*model.AccessData) store.StoreChannel); ok { + var r0 *model.AccessData + if rf, ok := ret.Get(0).(func(*model.AccessData) *model.AccessData); ok { r0 = rf(accessData) } else { 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 -func (_m *OAuthStore) SaveApp(app *model.OAuthApp) store.StoreChannel { +func (_m *OAuthStore) SaveApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError) { ret := _m.Called(app) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(*model.OAuthApp) store.StoreChannel); ok { + var r0 *model.OAuthApp + if rf, ok := ret.Get(0).(func(*model.OAuthApp) *model.OAuthApp); ok { r0 = rf(app) } else { 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 -func (_m *OAuthStore) SaveAuthData(authData *model.AuthData) store.StoreChannel { +func (_m *OAuthStore) SaveAuthData(authData *model.AuthData) (*model.AuthData, *model.AppError) { ret := _m.Called(authData) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(*model.AuthData) store.StoreChannel); ok { + var r0 *model.AuthData + if rf, ok := ret.Get(0).(func(*model.AuthData) *model.AuthData); ok { r0 = rf(authData) } else { 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 -func (_m *OAuthStore) UpdateAccessData(accessData *model.AccessData) store.StoreChannel { +func (_m *OAuthStore) UpdateAccessData(accessData *model.AccessData) (*model.AccessData, *model.AppError) { ret := _m.Called(accessData) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(*model.AccessData) store.StoreChannel); ok { + var r0 *model.AccessData + if rf, ok := ret.Get(0).(func(*model.AccessData) *model.AccessData); ok { r0 = rf(accessData) } else { 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 -func (_m *OAuthStore) UpdateApp(app *model.OAuthApp) store.StoreChannel { +func (_m *OAuthStore) UpdateApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppError) { ret := _m.Called(app) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(*model.OAuthApp) store.StoreChannel); ok { + var r0 *model.OAuthApp + if rf, ok := ret.Get(0).(func(*model.OAuthApp) *model.OAuthApp); ok { r0 = rf(app) } else { 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 } diff --git a/store/storetest/oauth_store.go b/store/storetest/oauth_store.go index 9ac49332bc..ba1b8f6439 100644 --- a/store/storetest/oauth_store.go +++ b/store/storetest/oauth_store.go @@ -8,6 +8,7 @@ import ( "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/store" + "github.com/stretchr/testify/assert" "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 a1.Id = model.NewId() - if err := (<-ss.OAuth().SaveApp(&a1)).Err; err == nil { - t.Fatal("Should have failed, cannot add an OAuth app cannot be save with an Id, it has to be updated") - } + _, err := ss.OAuth().SaveApp(&a1) + 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 a1.Id = "" - if err := (<-ss.OAuth().SaveApp(&a1)).Err; err == nil { - t.Fatal("Should have failed, app should be invalid cause it doesn' have a name set") - } + _, err = ss.OAuth().SaveApp(&a1) + require.NotNil(t, err, "Should have failed, app should be invalid cause it doesn' have a name set") // Save the app a1.Id = "" a1.Name = "TestApp" + model.NewId() - if err := (<-ss.OAuth().SaveApp(&a1)).Err; err != nil { - t.Fatal(err) - } + _, err = ss.OAuth().SaveApp(&a1) + require.Nil(t, err) } 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.CallbackUrls = []string{"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 - if err := (<-ss.OAuth().GetApp("fake0123456789abcderfgret1")).Err; err == nil { - t.Fatal("Should have failed. App does not exists") - } + _, err = ss.OAuth().GetApp("fake0123456789abcderfgret1") + require.NotNil(t, err, "Should have failed. App does not exists") - if err := (<-ss.OAuth().GetApp(a1.Id)).Err; err != nil { - t.Fatal(err) - } + _, err = ss.OAuth().GetApp(a1.Id) + require.Nil(t, err) // 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 { - if len(result.Data.([]*model.OAuthApp)) > 0 { - t.Fatal("Should have failed. Fake user hasn't created any apps") - } - } else { - t.Fatal(result.Err) - } + apps, err := ss.OAuth().GetAppByUser("fake0123456789abcderfgret1", 0, 1000) + require.Nil(t, err) + assert.Len(t, apps, 0, "Should have failed. Fake user hasn't created any apps") - if err := (<-ss.OAuth().GetAppByUser(a1.CreatorId, 0, 1000)).Err; err != nil { - t.Fatal(err) - } + _, err = ss.OAuth().GetAppByUser(a1.CreatorId, 0, 1000) + require.Nil(t, err) - if err := (<-ss.OAuth().GetApps(0, 1000)).Err; err != nil { - t.Fatal(err) - } + _, err = ss.OAuth().GetApps(0, 1000) + require.Nil(t, err) } 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.CallbackUrls = []string{"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 id := a1.Id @@ -106,32 +98,21 @@ func testOAuthStoreUpdateApp(t *testing.T, ss store.Store) { // Lets update the app by removing the name a1.Name = "" - if result := <-ss.OAuth().UpdateApp(&a1); result.Err == nil { - t.Fatal("Should have failed. App name is not set") - } + _, err = ss.OAuth().UpdateApp(&a1) + require.NotNil(t, err, "Should have failed. App name is not set") // Lets not find the app that we are trying to update a1.Id = "fake0123456789abcderfgret1" a1.Name = "NewName" - if result := <-ss.OAuth().UpdateApp(&a1); result.Err == nil { - t.Fatal("Should have failed. Not able to find the app") - } + _, err = ss.OAuth().UpdateApp(&a1) + require.NotNil(t, err, "Should have failed. Not able to find the app") a1.Id = id - if result := <-ss.OAuth().UpdateApp(&a1); result.Err != nil { - t.Fatal(result.Err) - } else { - ua1 := (result.Data.([2]*model.OAuthApp)[0]) - if ua1.Name != "NewName" { - 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") - } - } + ua, err := ss.OAuth().UpdateApp(&a1) + require.Nil(t, err) + require.Equal(t, ua.Name, "NewName", "name did not update") + require.NotEqual(t, ua.CreateAt, 1, "create at should not have updated") + require.NotEqual(t, ua.CreatorId, "12345678901234567890123456", "creator id should not have updated") } func testOAuthStoreSaveAccessData(t *testing.T, ss store.Store) { @@ -140,17 +121,15 @@ func testOAuthStoreSaveAccessData(t *testing.T, ss store.Store) { a1.UserId = model.NewId() // Lets try and save an incomplete access data - if err := (<-ss.OAuth().SaveAccessData(&a1)).Err; err == nil { - t.Fatal("Should have failed. Access data needs the token") - } + _, err := ss.OAuth().SaveAccessData(&a1) + require.NotNil(t, err, "Should have failed. Access data needs the token") a1.Token = model.NewId() a1.RefreshToken = model.NewId() a1.RedirectUri = "http://example.com" - if err := (<-ss.OAuth().SaveAccessData(&a1)).Err; err != nil { - t.Fatal(err) - } + _, err = ss.OAuth().SaveAccessData(&a1) + require.Nil(t, err) } 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.ExpiresAt = model.GetMillis() 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 refreshToken := a1.RefreshToken a1.RefreshToken = model.NewId() + "123" - if err := (<-ss.OAuth().UpdateAccessData(&a1)).Err; err == nil { - t.Fatal("Should have failed with invalid token") - } + _, err = ss.OAuth().UpdateAccessData(&a1) + require.NotNil(t, err, "Should have failed with invalid token") //Try to update to invalid RedirectUri a1.RefreshToken = model.NewId() a1.RedirectUri = "" - if err := (<-ss.OAuth().UpdateAccessData(&a1)).Err; err == nil { - t.Fatal("Should have failed with invalid Redirect URI") - } + _, err = ss.OAuth().UpdateAccessData(&a1) + require.NotNil(t, err, "Should have failed with invalid Redirect URI") // Should update fine a1.RedirectUri = "http://example.com" - if result := <-ss.OAuth().UpdateAccessData(&a1); result.Err != nil { - t.Fatal(result.Err) - } else { - ra1 := result.Data.(*model.AccessData) - if ra1.RefreshToken == refreshToken { - t.Fatal("refresh tokens didn't match") - } - } + ra1, err := ss.OAuth().UpdateAccessData(&a1) + require.Nil(t, err) + require.NotEqual(t, ra1.RefreshToken, refreshToken, "refresh tokens didn't match") } 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.ExpiresAt = model.GetMillis() 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 { - t.Fatal("Should have failed. There is no data with an invalid token") - } + _, err = ss.OAuth().GetAccessData("invalidToken") + 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 { - t.Fatal(result.Err) - } else { - ra1 := result.Data.(*model.AccessData) - if a1.Token != ra1.Token { - t.Fatal("tokens didn't match") - } - } + ra1, err := ss.OAuth().GetAccessData(a1.Token) + require.Nil(t, err) + assert.Equal(t, a1.Token, ra1.Token, "tokens didn't match") - if err := (<-ss.OAuth().GetPreviousAccessData(a1.UserId, a1.ClientId)).Err; err != nil { - t.Fatal(err) - } + _, err = ss.OAuth().GetPreviousAccessData(a1.UserId, a1.ClientId) + require.Nil(t, err) - if err := (<-ss.OAuth().GetPreviousAccessData("user", "junk")).Err; err != nil { - t.Fatal(err) - } + _, err = ss.OAuth().GetPreviousAccessData("user", "junk") + require.Nil(t, err) // Try to get the Access data using an invalid refresh token - if err := (<-ss.OAuth().GetAccessDataByRefreshToken(a1.Token)).Err; err == nil { - t.Fatal("Should have failed. There is no data with an invalid token") - } + _, err = ss.OAuth().GetAccessDataByRefreshToken(a1.Token) + require.NotNil(t, err, "Should have failed. There is no data with an invalid token") // Get the Access Data using the refresh token - if result := <-ss.OAuth().GetAccessDataByRefreshToken(a1.RefreshToken); result.Err != nil { - t.Fatal(result.Err) - } else { - ra1 := result.Data.(*model.AccessData) - if a1.RefreshToken != ra1.RefreshToken { - t.Fatal("tokens didn't match") - } - } + ra1, err = ss.OAuth().GetAccessDataByRefreshToken(a1.RefreshToken) + require.Nil(t, err) + assert.Equal(t, a1.RefreshToken, ra1.RefreshToken, "tokens didn't match") } 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.RefreshToken = model.NewId() 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 { - t.Fatal(err) - } + err = ss.OAuth().RemoveAccessData(a1.Token) + require.Nil(t, err) - if result := (<-ss.OAuth().GetPreviousAccessData(a1.UserId, a1.ClientId)); result.Err != nil { - } else { - if result.Data != nil { - t.Fatal("did not delete access token") - } - } + result, _ := ss.OAuth().GetPreviousAccessData(a1.UserId, a1.ClientId) + require.Nil(t, result, "did not delete access token") } 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.Code = model.NewId() a1.RedirectUri = "http://example.com" - if err := (<-ss.OAuth().SaveAuthData(&a1)).Err; err != nil { - t.Fatal(err) - } + _, err := ss.OAuth().SaveAuthData(&a1) + require.Nil(t, err) } 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.Code = model.NewId() 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 { - t.Fatal(err) - } + _, err = ss.OAuth().GetAuthData(a1.Code) + require.Nil(t, err) } 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.Code = model.NewId() 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 { - t.Fatal(err) - } + err = ss.OAuth().RemoveAuthData(a1.Code) + require.Nil(t, err) - if err := (<-ss.OAuth().GetAuthData(a1.Code)).Err; err == nil { - t.Fatal("should have errored - auth code removed") - } + _, err = ss.OAuth().GetAuthData(a1.Code) + require.NotNil(t, err, "should have errored - auth code removed") } 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.Code = model.NewId() 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 { - t.Fatal(err) - } + err = ss.OAuth().PermanentDeleteAuthDataByUser(a1.UserId) + require.Nil(t, err) } 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.CallbackUrls = []string{"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 - if result := <-ss.OAuth().GetAuthorizedApps("fake0123456789abcderfgret1", 0, 1000); result.Err == nil { - if len(result.Data.([]*model.OAuthApp)) > 0 { - t.Fatal("Should have failed. Fake user hasn't authorized the app") - } - } else { - t.Fatal(result.Err) - } + apps, err := ss.OAuth().GetAuthorizedApps("fake0123456789abcderfgret1", 0, 1000) + require.Nil(t, err) + assert.Len(t, apps, 0, "Should have failed. Fake user hasn't authorized the app") // allow the app p := model.Preference{} @@ -334,17 +285,12 @@ func testOAuthGetAuthorizedApps(t *testing.T, ss store.Store) { p.Category = model.PREFERENCE_CATEGORY_AUTHORIZED_OAUTH_APP p.Name = a1.Id p.Value = "true" - err := ss.Preference().Save(&model.Preferences{p}) + err = ss.Preference().Save(&model.Preferences{p}) require.Nil(t, err) - if result := <-ss.OAuth().GetAuthorizedApps(a1.CreatorId, 0, 1000); result.Err != nil { - t.Fatal(result.Err) - } else { - apps := result.Data.([]*model.OAuthApp) - if len(apps) == 0 { - t.Fatal("It should have return apps") - } - } + apps, err = ss.OAuth().GetAuthorizedApps(a1.CreatorId, 0, 1000) + require.Nil(t, err) + assert.NotEqual(t, len(apps), 0, "It should have return apps") } 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.CallbackUrls = []string{"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 p := model.Preference{} @@ -361,17 +308,12 @@ func testOAuthGetAccessDataByUserForApp(t *testing.T, ss store.Store) { p.Category = model.PREFERENCE_CATEGORY_AUTHORIZED_OAUTH_APP p.Name = a1.Id p.Value = "true" - err := ss.Preference().Save(&model.Preferences{p}) + err = ss.Preference().Save(&model.Preferences{p}) require.Nil(t, err) - if result := <-ss.OAuth().GetAuthorizedApps(a1.CreatorId, 0, 1000); result.Err != nil { - t.Fatal(result.Err) - } else { - apps := result.Data.([]*model.OAuthApp) - if len(apps) == 0 { - t.Fatal("It should have return apps") - } - } + apps, err := ss.OAuth().GetAuthorizedApps(a1.CreatorId, 0, 1000) + require.Nil(t, err) + assert.NotEqual(t, len(apps), 0, "It should have return apps") // save the token ad1 := model.AccessData{} @@ -381,18 +323,12 @@ func testOAuthGetAccessDataByUserForApp(t *testing.T, ss store.Store) { ad1.RefreshToken = model.NewId() ad1.RedirectUri = "http://example.com" - if err := (<-ss.OAuth().SaveAccessData(&ad1)).Err; err != nil { - t.Fatal(err) - } + _, err = ss.OAuth().SaveAccessData(&ad1) + require.Nil(t, err) - if result := <-ss.OAuth().GetAccessDataByUserForApp(a1.CreatorId, a1.Id); result.Err != nil { - t.Fatal(result.Err) - } else { - accessData := result.Data.([]*model.AccessData) - if len(accessData) == 0 { - t.Fatal("It should have return access data") - } - } + accessData, err := ss.OAuth().GetAccessDataByUserForApp(a1.CreatorId, a1.Id) + require.Nil(t, err) + assert.NotEqual(t, len(accessData), 0, "It should have return access data") } 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.CallbackUrls = []string{"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 - if err := (<-ss.OAuth().DeleteApp("fakeclientId")).Err; err != nil { - t.Fatal(err) - } + err = ss.OAuth().DeleteApp("fakeclientId") + require.Nil(t, err) s1 := &model.Session{} s1.UserId = model.NewId() s1.Token = model.NewId() s1.IsOAuth = true - s1, err := ss.Session().Save(s1) + s1, err = ss.Session().Save(s1) require.Nil(t, err) ad1 := model.AccessData{} @@ -423,17 +359,16 @@ func testOAuthStoreDeleteApp(t *testing.T, ss store.Store) { ad1.RefreshToken = model.NewId() 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 { - t.Fatal(err) - } + err = ss.OAuth().DeleteApp(a1.Id) + 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") } - if err := (<-ss.OAuth().GetAccessData(s1.Token)).Err; err == nil { - t.Fatal("should error - access data should be deleted") - } + _, err = ss.OAuth().GetAccessData(s1.Token) + require.NotNil(t, err, "should error - access data should be deleted") }