From 8e19ba029f889519d93cf272960dce858971106c Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 18 Oct 2017 15:36:43 -0700 Subject: Reduce utils.Cfg references (#7650) * app.UpdateConfig method * test fix * another test fix * the config override option as-was is just error prone, remove it for now * derp --- api/oauth_test.go | 106 +++++++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 53 deletions(-) (limited to 'api/oauth_test.go') diff --git a/api/oauth_test.go b/api/oauth_test.go index 0f809dfe6..1510e3520 100644 --- a/api/oauth_test.go +++ b/api/oauth_test.go @@ -25,14 +25,14 @@ func TestOAuthRegisterApp(t *testing.T) { oauthApp := &model.OAuthApp{Name: "TestApp" + model.NewId(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}, IsTrusted: true} - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = false - if !utils.Cfg.ServiceSettings.EnableOAuthServiceProvider { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = false }) + if !th.App.Config().ServiceSettings.EnableOAuthServiceProvider { if _, err := Client.RegisterApp(oauthApp); err == nil { t.Fatal("should have failed - oauth providing turned off") } } - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) // calling the endpoint without an app if _, err := Client.DoApiPost("/oauth/register", ""); err == nil { @@ -88,12 +88,12 @@ func TestOAuthRegisterApp(t *testing.T) { t.Fatal("should have failed. not enough permissions") } - adminOnly := *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations + adminOnly := *th.App.Config().ServiceSettings.EnableOnlyAdminIntegrations defer func() { - *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = adminOnly + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOnlyAdminIntegrations = adminOnly }) utils.SetDefaultRolesBasedOnConfig() }() - *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = false + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOnlyAdminIntegrations = false }) utils.SetDefaultRolesBasedOnConfig() th.LoginBasic() @@ -115,18 +115,18 @@ func TestOAuthAllow(t *testing.T) { Client := th.BasicClient AdminClient := th.SystemAdminClient - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) oauthApp := &model.OAuthApp{Name: "TestApp" + model.NewId(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}} oauthApp = AdminClient.Must(AdminClient.RegisterApp(oauthApp)).Data.(*model.OAuthApp) state := "123" - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = false + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = false }) if _, err := Client.AllowOAuth(model.AUTHCODE_RESPONSE_TYPE, oauthApp.Id, oauthApp.CallbackUrls[0], "all", state); err == nil { t.Fatal("should have failed - oauth providing turned off") } - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) if result, err := Client.AllowOAuth(model.AUTHCODE_RESPONSE_TYPE, oauthApp.Id, oauthApp.CallbackUrls[0], "all", state); err != nil { t.Fatal(err) @@ -202,21 +202,21 @@ func TestOAuthGetAppsByUser(t *testing.T) { Client := th.BasicClient AdminClient := th.SystemAdminClient - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = false - if !utils.Cfg.ServiceSettings.EnableOAuthServiceProvider { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = false }) + if !th.App.Config().ServiceSettings.EnableOAuthServiceProvider { if _, err := Client.GetOAuthAppsByUser(); err == nil { t.Fatal("should have failed - oauth providing turned off") } } - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) if _, err := Client.GetOAuthAppsByUser(); err == nil { t.Fatal("Should have failed.") } - *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = false + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOnlyAdminIntegrations = false }) utils.SetDefaultRolesBasedOnConfig() if result, err := Client.GetOAuthAppsByUser(); err != nil { @@ -274,15 +274,15 @@ func TestOAuthGetAppInfo(t *testing.T) { Client := th.BasicClient AdminClient := th.SystemAdminClient - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = false - if !utils.Cfg.ServiceSettings.EnableOAuthServiceProvider { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = false }) + if !th.App.Config().ServiceSettings.EnableOAuthServiceProvider { if _, err := Client.GetOAuthAppInfo("fakeId"); err == nil { t.Fatal("should have failed - oauth providing turned off") } } - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) oauthApp := &model.OAuthApp{Name: "TestApp5" + model.NewId(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}} @@ -304,15 +304,15 @@ func TestOAuthGetAuthorizedApps(t *testing.T) { Client := th.BasicClient AdminClient := th.SystemAdminClient - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = false - if !utils.Cfg.ServiceSettings.EnableOAuthServiceProvider { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = false }) + if !th.App.Config().ServiceSettings.EnableOAuthServiceProvider { if _, err := Client.GetOAuthAuthorizedApps(); err == nil { t.Fatal("should have failed - oauth providing turned off") } } - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) oauthApp := &model.OAuthApp{Name: "TestApp5" + model.NewId(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}} oauthApp = AdminClient.Must(AdminClient.RegisterApp(oauthApp)).Data.(*model.OAuthApp) @@ -339,15 +339,15 @@ func TestOAuthDeauthorizeApp(t *testing.T) { Client := th.BasicClient AdminClient := th.SystemAdminClient - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = false - if !utils.Cfg.ServiceSettings.EnableOAuthServiceProvider { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = false }) + if !th.App.Config().ServiceSettings.EnableOAuthServiceProvider { if err := Client.OAuthDeauthorizeApp(model.NewId()); err == nil { t.Fatal("should have failed - oauth providing turned off") } } - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) oauthApp := &model.OAuthApp{Name: "TestApp5" + model.NewId(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}} @@ -392,15 +392,15 @@ func TestOAuthRegenerateAppSecret(t *testing.T) { Client := th.BasicClient AdminClient := th.SystemAdminClient - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = false - if !utils.Cfg.ServiceSettings.EnableOAuthServiceProvider { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = false }) + if !th.App.Config().ServiceSettings.EnableOAuthServiceProvider { if _, err := AdminClient.RegenerateOAuthAppSecret(model.NewId()); err == nil { t.Fatal("should have failed - oauth providing turned off") } } - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) oauthApp := &model.OAuthApp{Name: "TestApp6" + model.NewId(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}} @@ -435,16 +435,16 @@ func TestOAuthDeleteApp(t *testing.T) { Client := th.BasicClient AdminClient := th.SystemAdminClient - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = false - if !utils.Cfg.ServiceSettings.EnableOAuthServiceProvider { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = false }) + if !th.App.Config().ServiceSettings.EnableOAuthServiceProvider { if _, err := Client.DeleteOAuthApp("fakeId"); err == nil { t.Fatal("should have failed - oauth providing turned off") } } - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true - *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = false + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOnlyAdminIntegrations = false }) utils.SetDefaultRolesBasedOnConfig() oauthApp := &model.OAuthApp{Name: "TestApp5" + model.NewId(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}} @@ -567,27 +567,27 @@ func TestOAuthAccessToken(t *testing.T) { Client := th.BasicClient - enableOAuth := utils.Cfg.ServiceSettings.EnableOAuthServiceProvider - adminOnly := *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations + enableOAuth := th.App.Config().ServiceSettings.EnableOAuthServiceProvider + adminOnly := *th.App.Config().ServiceSettings.EnableOnlyAdminIntegrations defer func() { - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = enableOAuth - *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = adminOnly + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = enableOAuth }) + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOnlyAdminIntegrations = adminOnly }) utils.SetDefaultRolesBasedOnConfig() }() - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true - *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = false + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOnlyAdminIntegrations = false }) utils.SetDefaultRolesBasedOnConfig() oauthApp := &model.OAuthApp{Name: "TestApp5" + model.NewId(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}} oauthApp = Client.Must(Client.RegisterApp(oauthApp)).Data.(*model.OAuthApp) - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = false + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = false }) data := url.Values{"grant_type": []string{"junk"}, "client_id": []string{"12345678901234567890123456"}, "client_secret": []string{"12345678901234567890123456"}, "code": []string{"junk"}, "redirect_uri": []string{oauthApp.CallbackUrls[0]}} if _, err := Client.GetAccessToken(data); err == nil { t.Fatal("should have failed - oauth providing turned off") } - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) redirect := Client.Must(Client.AllowOAuth(model.AUTHCODE_RESPONSE_TYPE, oauthApp.Id, oauthApp.CallbackUrls[0], "all", "123")).Data.(map[string]string)["redirect"] rurl, _ := url.Parse(redirect) @@ -788,19 +788,19 @@ func TestOAuthComplete(t *testing.T) { closeBody(r) } - utils.Cfg.GitLabSettings.Enable = true + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.Enable = true }) if r, err := HttpGet(Client.Url+"/login/gitlab/complete?code=123&state=!#$#F@#Yˆ&~ñ", Client.HttpClient, "", true); err == nil { t.Fatal("should have failed - gitlab disabled") closeBody(r) } - utils.Cfg.GitLabSettings.AuthEndpoint = Client.Url + "/oauth/authorize" - utils.Cfg.GitLabSettings.Id = model.NewId() + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.AuthEndpoint = Client.Url + "/oauth/authorize" }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.Id = model.NewId() }) stateProps := map[string]string{} stateProps["action"] = model.OAUTH_ACTION_LOGIN stateProps["team_id"] = th.BasicTeam.Id - stateProps["redirect_to"] = utils.Cfg.GitLabSettings.AuthEndpoint + stateProps["redirect_to"] = th.App.Config().GitLabSettings.AuthEndpoint state := base64.StdEncoding.EncodeToString([]byte(model.MapToJson(stateProps))) if r, err := HttpGet(Client.Url+"/login/gitlab/complete?code=123&state="+url.QueryEscape(state), Client.HttpClient, "", true); err == nil { @@ -808,7 +808,7 @@ func TestOAuthComplete(t *testing.T) { closeBody(r) } - stateProps["hash"] = utils.HashSha256(utils.Cfg.GitLabSettings.Id) + stateProps["hash"] = utils.HashSha256(th.App.Config().GitLabSettings.Id) state = base64.StdEncoding.EncodeToString([]byte(model.MapToJson(stateProps))) if r, err := HttpGet(Client.Url+"/login/gitlab/complete?code=123&state="+url.QueryEscape(state), Client.HttpClient, "", true); err == nil { t.Fatal("should have failed - no connection") @@ -816,14 +816,14 @@ func TestOAuthComplete(t *testing.T) { } // We are going to use mattermost as the provider emulating gitlab - utils.Cfg.ServiceSettings.EnableOAuthServiceProvider = true + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) - adminOnly := *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations + adminOnly := *th.App.Config().ServiceSettings.EnableOnlyAdminIntegrations defer func() { - *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = adminOnly + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOnlyAdminIntegrations = adminOnly }) utils.SetDefaultRolesBasedOnConfig() }() - *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = false + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOnlyAdminIntegrations = false }) utils.SetDefaultRolesBasedOnConfig() oauthApp := &model.OAuthApp{ @@ -838,11 +838,11 @@ func TestOAuthComplete(t *testing.T) { } oauthApp = Client.Must(Client.RegisterApp(oauthApp)).Data.(*model.OAuthApp) - utils.Cfg.GitLabSettings.Id = oauthApp.Id - utils.Cfg.GitLabSettings.Secret = oauthApp.ClientSecret - utils.Cfg.GitLabSettings.AuthEndpoint = Client.Url + "/oauth/authorize" - utils.Cfg.GitLabSettings.TokenEndpoint = Client.Url + "/oauth/access_token" - utils.Cfg.GitLabSettings.UserApiEndpoint = Client.ApiUrl + "/users/me" + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.Id = oauthApp.Id }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.Secret = oauthApp.ClientSecret }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.AuthEndpoint = Client.Url + "/oauth/authorize" }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.TokenEndpoint = Client.Url + "/oauth/access_token" }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.UserApiEndpoint = Client.ApiUrl + "/users/me" }) provider := &MattermostTestProvider{} @@ -851,8 +851,8 @@ func TestOAuthComplete(t *testing.T) { code := rurl.Query().Get("code") stateProps["action"] = model.OAUTH_ACTION_EMAIL_TO_SSO delete(stateProps, "team_id") - stateProps["redirect_to"] = utils.Cfg.GitLabSettings.AuthEndpoint - stateProps["hash"] = utils.HashSha256(utils.Cfg.GitLabSettings.Id) + stateProps["redirect_to"] = th.App.Config().GitLabSettings.AuthEndpoint + stateProps["hash"] = utils.HashSha256(th.App.Config().GitLabSettings.Id) stateProps["redirect_to"] = "/oauth/authorize" state = base64.StdEncoding.EncodeToString([]byte(model.MapToJson(stateProps))) if r, err := HttpGet(Client.Url+"/login/"+model.SERVICE_GITLAB+"/complete?code="+url.QueryEscape(code)+"&state="+url.QueryEscape(state), Client.HttpClient, "", false); err == nil { -- cgit v1.2.3-1-g7c22