MM-40542: Verify config setting in api (#2170)

* verify config setting in api

* update message

* fix lint

* update/add unit test, throw error

* fix typo
This commit is contained in:
Scott Bishel 2022-01-28 10:35:38 -07:00 committed by GitHub
parent 690dc365f5
commit bccfbd3d2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 10 deletions

View file

@ -1170,13 +1170,22 @@ func (a *API) handlePostSharing(w http.ResponseWriter, r *http.Request) {
auditRec.AddMeta("shareID", sharing.ID)
auditRec.AddMeta("enabled", sharing.Enabled)
// Stamp ModifiedBy
ctx := r.Context()
session := ctx.Value(sessionContextKey).(*model.Session)
userID := session.UserID
if userID == SingleUser {
userID = ""
}
if !a.app.GetClientConfig().EnablePublicSharedBoards {
a.logger.Info(
"Attempt to turn on sharing for board via API failed, sharing off in configuration.",
mlog.String("boardID", sharing.ID),
mlog.String("userID", userID))
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "Turning on sharing for board failed, see log for details.", nil)
return
}
sharing.ModifiedBy = userID
err = a.app.UpsertSharing(*container, sharing)

View file

@ -22,7 +22,7 @@ func TestSharing(t *testing.T) {
require.False(t, sharing.Enabled)
})
t.Run("POST sharing", func(t *testing.T) {
t.Run("POST sharing, config = false", func(t *testing.T) {
sharing := model.Sharing{
ID: rootID,
Token: token,
@ -30,17 +30,41 @@ func TestSharing(t *testing.T) {
UpdateAt: 1,
}
// it will fail with default config
success, resp := th.Client.PostSharing(sharing)
require.False(t, success)
require.Error(t, resp.Error)
t.Run("GET sharing", func(t *testing.T) {
sharing, resp := th.Client.GetSharing(rootID)
// Expect no error, but no Id returned
require.NoError(t, resp.Error)
require.NotNil(t, sharing)
require.Equal(t, "", sharing.ID)
})
})
t.Run("POST sharing, config = true", func(t *testing.T) {
th.Server.Config().EnablePublicSharedBoards = true
sharing := model.Sharing{
ID: rootID,
Token: token,
Enabled: true,
UpdateAt: 1,
}
// it will succeed with updated config
success, resp := th.Client.PostSharing(sharing)
require.True(t, success)
require.NoError(t, resp.Error)
})
t.Run("GET sharing", func(t *testing.T) {
sharing, resp := th.Client.GetSharing(rootID)
require.NoError(t, resp.Error)
require.NotNil(t, sharing)
require.Equal(t, sharing.ID, rootID)
require.True(t, sharing.Enabled)
require.Equal(t, sharing.Token, token)
t.Run("GET sharing", func(t *testing.T) {
sharing, resp := th.Client.GetSharing(rootID)
require.NoError(t, resp.Error)
require.NotNil(t, sharing)
require.Equal(t, sharing.ID, rootID)
require.True(t, sharing.Enabled)
require.Equal(t, sharing.Token, token)
})
})
}