Refactored to rename "service terms" to "terms of service" (#9581)

* #124 renamed identififers from service terms to terms of service

* #124 renamed identififers from service terms to terms of service

* 124 renamed ServiceTerms model to TermsOfService

* 124 Renamed EnableCustomServiceTerms feature flag to EnableCustomTermsOfService

* 124 Renamed EnableCustomServiceTerms feature flag to EnableCustomTermsOfService

* #124 fixed formatting

* #124 fixed formatting

* #132 renamed table ServiceTerms to TermsOfService

* #124 renamed some missed files from 'service_terms' to 'terms_of_service'

* #124 removed fixed TODOs

* drop migrate of ServiceTerms table, since backporting

* s/ServiceTerms/TermsOfService/ in tests

* s/AcceptedServiceTermsId/AcceptedTermsOfServiceId/

Change the model attribute, even though the column name will eventually be removed.

* s/accepted_service_terms_id/accepted_terms_of_service_id/ to match redux

* s/serviceTerms/termsOfService

* rename column too, and add max size constraint

* s/EnableCustomServiceTerms/EnableCustomTermsOfService
Этот коммит содержится в:
Harshil Sharma
2018-10-10 00:55:47 +00:00
коммит произвёл Jesse Hallam
родитель 59319b7915
Коммит bffcccf99d
37 изменённых файлов: 630 добавлений и 629 удалений

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

@@ -108,7 +108,7 @@ type Routes struct {
Webrtc *mux.Router // 'api/v4/webrtc'
ServiceTerms *mux.Router // 'api/v4/service_terms
TermsOfService *mux.Router // 'api/v4/terms_of_service
}
type API struct {
@@ -205,7 +205,7 @@ func Init(a *app.App, root *mux.Router) *API {
api.BaseRoutes.Image = api.BaseRoutes.ApiRoot.PathPrefix("/image").Subrouter()
api.BaseRoutes.ServiceTerms = api.BaseRoutes.ApiRoot.PathPrefix("/terms_of_service").Subrouter()
api.BaseRoutes.TermsOfService = api.BaseRoutes.ApiRoot.PathPrefix("/terms_of_service").Subrouter()
api.InitUser()
api.InitTeam()
@@ -235,7 +235,7 @@ func Init(a *app.App, root *mux.Router) *API {
api.InitRole()
api.InitScheme()
api.InitImage()
api.InitServiceTerms()
api.InitTermsOfService()
root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404))

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

@@ -1,64 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api4
import (
"github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/model"
"net/http"
)
func (api *API) InitServiceTerms() {
api.BaseRoutes.ServiceTerms.Handle("", api.ApiSessionRequired(getServiceTerms)).Methods("GET")
api.BaseRoutes.ServiceTerms.Handle("", api.ApiSessionRequired(createServiceTerms)).Methods("POST")
}
func getServiceTerms(c *Context, w http.ResponseWriter, r *http.Request) {
serviceTerms, err := c.App.GetLatestServiceTerms()
if err != nil {
c.Err = err
return
}
w.Write([]byte(serviceTerms.ToJson()))
}
func createServiceTerms(c *Context, w http.ResponseWriter, r *http.Request) {
if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
return
}
if license := c.App.License(); license == nil || !*license.Features.CustomTermsOfService {
c.Err = model.NewAppError("createServiceTerms", "api.create_service_terms.custom_service_terms_disabled.app_error", nil, "", http.StatusBadRequest)
return
}
props := model.MapFromJson(r.Body)
text := props["text"]
userId := c.Session.UserId
if text == "" {
c.Err = model.NewAppError("Config.IsValid", "api.create_service_terms.empty_text.app_error", nil, "", http.StatusBadRequest)
return
}
oldServiceTerms, err := c.App.GetLatestServiceTerms()
if err != nil && err.Id != app.ERROR_SERVICE_TERMS_NO_ROWS_FOUND {
c.Err = err
return
}
if oldServiceTerms == nil || oldServiceTerms.Text != text {
serviceTerms, err := c.App.CreateServiceTerms(text, userId)
if err != nil {
c.Err = err
return
}
w.Write([]byte(serviceTerms.ToJson()))
} else {
w.Write([]byte(oldServiceTerms.ToJson()))
}
}

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

@@ -1,53 +0,0 @@
package api4
import (
"github.com/mattermost/mattermost-server/model"
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetServiceTerms(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
Client := th.Client
_, err := th.App.CreateServiceTerms("abc", th.BasicUser.Id)
if err != nil {
t.Fatal(err)
}
serviceTerms, resp := Client.GetServiceTerms("")
CheckNoError(t, resp)
assert.NotNil(t, serviceTerms)
assert.Equal(t, "abc", serviceTerms.Text)
assert.NotEmpty(t, serviceTerms.Id)
assert.NotEmpty(t, serviceTerms.CreateAt)
}
func TestCreateServiceTerms(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
Client := th.Client
_, resp := Client.CreateServiceTerms("service terms new", th.BasicUser.Id)
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
}
func TestCreateServiceTermsAdminUser(t *testing.T) {
th := Setup().InitSystemAdmin()
defer th.TearDown()
Client := th.SystemAdminClient
serviceTerms, resp := Client.CreateServiceTerms("service terms new", th.SystemAdminUser.Id)
CheckErrorMessage(t, resp, "api.create_service_terms.custom_service_terms_disabled.app_error")
th.App.SetLicense(model.NewTestLicense("EnableCustomServiceTerms"))
serviceTerms, resp = Client.CreateServiceTerms("service terms new_2", th.SystemAdminUser.Id)
CheckNoError(t, resp)
assert.NotEmpty(t, serviceTerms.Id)
assert.NotEmpty(t, serviceTerms.CreateAt)
assert.Equal(t, "service terms new_2", serviceTerms.Text)
assert.Equal(t, th.SystemAdminUser.Id, serviceTerms.UserId)
}

64
api4/terms_of_service.go Обычный файл
Просмотреть файл

@@ -0,0 +1,64 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api4
import (
"github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/model"
"net/http"
)
func (api *API) InitTermsOfService() {
api.BaseRoutes.TermsOfService.Handle("", api.ApiSessionRequired(getTermsOfService)).Methods("GET")
api.BaseRoutes.TermsOfService.Handle("", api.ApiSessionRequired(createTermsOfService)).Methods("POST")
}
func getTermsOfService(c *Context, w http.ResponseWriter, r *http.Request) {
termsOfService, err := c.App.GetLatestTermsOfService()
if err != nil {
c.Err = err
return
}
w.Write([]byte(termsOfService.ToJson()))
}
func createTermsOfService(c *Context, w http.ResponseWriter, r *http.Request) {
if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
return
}
if license := c.App.License(); license == nil || !*license.Features.CustomTermsOfService {
c.Err = model.NewAppError("createTermsOfService", "api.create_terms_of_service.custom_terms_of_service_disabled.app_error", nil, "", http.StatusBadRequest)
return
}
props := model.MapFromJson(r.Body)
text := props["text"]
userId := c.Session.UserId
if text == "" {
c.Err = model.NewAppError("Config.IsValid", "api.create_terms_of_service.empty_text.app_error", nil, "", http.StatusBadRequest)
return
}
oldTermsOfService, err := c.App.GetLatestTermsOfService()
if err != nil && err.Id != app.ERROR_TERMS_OF_SERVICE_NO_ROWS_FOUND {
c.Err = err
return
}
if oldTermsOfService == nil || oldTermsOfService.Text != text {
termsOfService, err := c.App.CreateTermsOfService(text, userId)
if err != nil {
c.Err = err
return
}
w.Write([]byte(termsOfService.ToJson()))
} else {
w.Write([]byte(oldTermsOfService.ToJson()))
}
}

53
api4/terms_of_service_test.go Обычный файл
Просмотреть файл

@@ -0,0 +1,53 @@
package api4
import (
"github.com/mattermost/mattermost-server/model"
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetTermsOfService(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
Client := th.Client
_, err := th.App.CreateTermsOfService("abc", th.BasicUser.Id)
if err != nil {
t.Fatal(err)
}
termsOfService, resp := Client.GetTermsOfService("")
CheckNoError(t, resp)
assert.NotNil(t, termsOfService)
assert.Equal(t, "abc", termsOfService.Text)
assert.NotEmpty(t, termsOfService.Id)
assert.NotEmpty(t, termsOfService.CreateAt)
}
func TestCreateTermsOfService(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
Client := th.Client
_, resp := Client.CreateTermsOfService("terms of service new", th.BasicUser.Id)
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
}
func TestCreateTermsOfServiceAdminUser(t *testing.T) {
th := Setup().InitSystemAdmin()
defer th.TearDown()
Client := th.SystemAdminClient
termsOfService, resp := Client.CreateTermsOfService("terms of service new", th.SystemAdminUser.Id)
CheckErrorMessage(t, resp, "api.create_terms_of_service.custom_terms_of_service_disabled.app_error")
th.App.SetLicense(model.NewTestLicense("EnableCustomTermsOfService"))
termsOfService, resp = Client.CreateTermsOfService("terms of service new_2", th.SystemAdminUser.Id)
CheckNoError(t, resp)
assert.NotEmpty(t, termsOfService.Id)
assert.NotEmpty(t, termsOfService.CreateAt)
assert.Equal(t, "terms of service new_2", termsOfService.Text)
assert.Equal(t, th.SystemAdminUser.Id, termsOfService.UserId)
}

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

@@ -41,7 +41,7 @@ func (api *API) InitUser() {
api.BaseRoutes.Users.Handle("/password/reset/send", api.ApiHandler(sendPasswordReset)).Methods("POST")
api.BaseRoutes.Users.Handle("/email/verify", api.ApiHandler(verifyUserEmail)).Methods("POST")
api.BaseRoutes.Users.Handle("/email/verify/send", api.ApiHandler(sendVerificationEmail)).Methods("POST")
api.BaseRoutes.User.Handle("/terms_of_service", api.ApiSessionRequired(registerServiceTermsAction)).Methods("POST")
api.BaseRoutes.User.Handle("/terms_of_service", api.ApiSessionRequired(registerTermsOfServiceAction)).Methods("POST")
api.BaseRoutes.User.Handle("/auth", api.ApiSessionRequiredTrustRequester(updateUserAuth)).Methods("PUT")
@@ -1615,23 +1615,23 @@ func enableUserAccessToken(c *Context, w http.ResponseWriter, r *http.Request) {
ReturnStatusOK(w)
}
func registerServiceTermsAction(c *Context, w http.ResponseWriter, r *http.Request) {
func registerTermsOfServiceAction(c *Context, w http.ResponseWriter, r *http.Request) {
props := model.StringInterfaceFromJson(r.Body)
userId := c.Session.UserId
serviceTermsId := props["serviceTermsId"].(string)
termsOfServiceId := props["termsOfServiceId"].(string)
accepted := props["accepted"].(bool)
if _, err := c.App.GetServiceTerms(serviceTermsId); err != nil {
if _, err := c.App.GetTermsOfService(termsOfServiceId); err != nil {
c.Err = err
return
}
if err := c.App.RecordUserServiceTermsAction(userId, serviceTermsId, accepted); err != nil {
if err := c.App.RecordUserTermsOfServiceAction(userId, termsOfServiceId, accepted); err != nil {
c.Err = err
return
}
c.LogAudit("ServiceTermsId=" + serviceTermsId + ", accepted=" + strconv.FormatBool(accepted))
c.LogAudit("TermsOfServiceId=" + termsOfServiceId + ", accepted=" + strconv.FormatBool(accepted))
ReturnStatusOK(w)
}

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

@@ -3069,20 +3069,20 @@ func TestGetUsersByStatus(t *testing.T) {
})
}
func TestRegisterServiceTermsAction(t *testing.T) {
func TestRegisterTermsOfServiceAction(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
Client := th.Client
success, resp := Client.RegisterServiceTermsAction(th.BasicUser.Id, "st_1", true)
CheckErrorMessage(t, resp, "store.sql_service_terms_store.get.no_rows.app_error")
success, resp := Client.RegisteTermsOfServiceAction(th.BasicUser.Id, "st_1", true)
CheckErrorMessage(t, resp, "store.sql_terms_of_service_store.get.no_rows.app_error")
serviceTerms, err := th.App.CreateServiceTerms("service terms", th.BasicUser.Id)
termsOfService, err := th.App.CreateTermsOfService("terms of service", th.BasicUser.Id)
if err != nil {
t.Fatal(err)
}
success, resp = Client.RegisterServiceTermsAction(th.BasicUser.Id, serviceTerms.Id, true)
success, resp = Client.RegisteTermsOfServiceAction(th.BasicUser.Id, termsOfService.Id, true)
CheckNoError(t, resp)
assert.True(t, *success)
@@ -3091,5 +3091,5 @@ func TestRegisterServiceTermsAction(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, user.AcceptedServiceTermsId, serviceTerms.Id)
assert.Equal(t, user.AcceptedTermsOfServiceId, termsOfService.Id)
}