Align GoChat with Chatwoot frontend contracts
This commit is contained in:
@@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -33,12 +34,7 @@ func (s *AccountSamlSettingsHandlerTestSuite) SetupSuite() {
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
accountGroup := r.Group("/api/v1/accounts/:account_id")
|
||||
accountGroup.GET("/saml_settings", s.handler.Get)
|
||||
accountGroup.POST("/saml_settings", s.handler.Create)
|
||||
accountGroup.PUT("/saml_settings/:saml_setting_id", s.handler.Update)
|
||||
accountGroup.DELETE("/saml_settings/:saml_setting_id", s.handler.Delete)
|
||||
accountGroup.POST("/saml_settings/:saml_setting_id/toggle_active", s.handler.ToggleActive)
|
||||
RegisterAccountSamlSettingsRoutes(r.Group("/api/v1/accounts/:account_id/saml_settings"), s.handler)
|
||||
s.router = r
|
||||
|
||||
s.account = &model.Account{Name: "TestAccount"}
|
||||
@@ -79,6 +75,71 @@ func (s *AccountSamlSettingsHandlerTestSuite) TestGet_Success() {
|
||||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/saml_settings", s.account.ID), nil)
|
||||
s.router.ServeHTTP(w, req)
|
||||
s.Equal(http.StatusOK, w.Code)
|
||||
|
||||
var payload map[string]interface{}
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &payload))
|
||||
s.Equal("https://sso.example.com", payload["sso_url"])
|
||||
s.Equal("https://sso.example.com", payload["idp_sso_target_url"])
|
||||
s.Equal("entity-id", payload["idp_entity_id"])
|
||||
s.NotContains(payload, "data")
|
||||
}
|
||||
|
||||
func (s *AccountSamlSettingsHandlerTestSuite) TestChatwootFrontendCollectionCRUDPayloads() {
|
||||
createBody := bytes.NewBufferString(`{"saml_settings":{"sso_url":"https://idp.example.com/saml","certificate":"-----BEGIN CERTIFICATE-----chatwoot-----END CERTIFICATE-----","idp_entity_id":"chatwoot-idp","role_mappings":{}}}`)
|
||||
createReq := httptest.NewRequest(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/saml_settings", s.account.ID), createBody)
|
||||
createReq.Header.Set("Content-Type", "application/json")
|
||||
createRecorder := httptest.NewRecorder()
|
||||
s.router.ServeHTTP(createRecorder, createReq)
|
||||
s.Equal(http.StatusCreated, createRecorder.Code)
|
||||
|
||||
var created map[string]interface{}
|
||||
s.Require().NoError(json.Unmarshal(createRecorder.Body.Bytes(), &created))
|
||||
s.NotZero(created["id"])
|
||||
s.Equal("https://idp.example.com/saml", created["sso_url"])
|
||||
s.Equal("https://idp.example.com/saml", created["idp_sso_target_url"])
|
||||
s.Equal("-----BEGIN CERTIFICATE-----chatwoot-----END CERTIFICATE-----", created["certificate"])
|
||||
s.Equal("-----BEGIN CERTIFICATE-----chatwoot-----END CERTIFICATE-----", created["idp_certificate"])
|
||||
s.Equal("chatwoot-idp", created["idp_entity_id"])
|
||||
s.NotEmpty(created["fingerprint"])
|
||||
s.NotContains(created, "data")
|
||||
s.NotContains(created, "success")
|
||||
|
||||
getReq := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/saml_settings", s.account.ID), nil)
|
||||
getRecorder := httptest.NewRecorder()
|
||||
s.router.ServeHTTP(getRecorder, getReq)
|
||||
s.Equal(http.StatusOK, getRecorder.Code)
|
||||
|
||||
var fetched map[string]interface{}
|
||||
s.Require().NoError(json.Unmarshal(getRecorder.Body.Bytes(), &fetched))
|
||||
s.Equal(created["id"], fetched["id"])
|
||||
s.Equal("https://idp.example.com/saml", fetched["sso_url"])
|
||||
|
||||
updateBody := bytes.NewBufferString(`{"saml_settings":{"sso_url":"https://idp.example.com/updated","certificate":"updated-certificate","idp_entity_id":"updated-idp"}}`)
|
||||
updateReq := httptest.NewRequest(http.MethodPut, fmt.Sprintf("/api/v1/accounts/%d/saml_settings", s.account.ID), updateBody)
|
||||
updateReq.Header.Set("Content-Type", "application/json")
|
||||
updateRecorder := httptest.NewRecorder()
|
||||
s.router.ServeHTTP(updateRecorder, updateReq)
|
||||
s.Equal(http.StatusOK, updateRecorder.Code)
|
||||
|
||||
var updated map[string]interface{}
|
||||
s.Require().NoError(json.Unmarshal(updateRecorder.Body.Bytes(), &updated))
|
||||
s.Equal(created["id"], updated["id"])
|
||||
s.Equal("https://idp.example.com/updated", updated["sso_url"])
|
||||
s.Equal("https://idp.example.com/updated", updated["idp_sso_target_url"])
|
||||
s.Equal("updated-certificate", updated["certificate"])
|
||||
s.Equal("updated-certificate", updated["idp_certificate"])
|
||||
s.Equal("updated-idp", updated["idp_entity_id"])
|
||||
s.NotContains(updated, "data")
|
||||
|
||||
deleteReq := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/api/v1/accounts/%d/saml_settings", s.account.ID), nil)
|
||||
deleteRecorder := httptest.NewRecorder()
|
||||
s.router.ServeHTTP(deleteRecorder, deleteReq)
|
||||
s.Equal(http.StatusOK, deleteRecorder.Code)
|
||||
|
||||
afterDeleteReq := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/saml_settings", s.account.ID), nil)
|
||||
afterDeleteRecorder := httptest.NewRecorder()
|
||||
s.router.ServeHTTP(afterDeleteRecorder, afterDeleteReq)
|
||||
s.Equal(http.StatusNotFound, afterDeleteRecorder.Code)
|
||||
}
|
||||
|
||||
func (s *AccountSamlSettingsHandlerTestSuite) TestCreate_InvalidAccountID() {
|
||||
@@ -111,7 +172,7 @@ func (s *AccountSamlSettingsHandlerTestSuite) TestCreate_MissingRequiredFields()
|
||||
func (s *AccountSamlSettingsHandlerTestSuite) TestUpdate_InvalidAccountID() {
|
||||
w := httptest.NewRecorder()
|
||||
body := bytes.NewBufferString(`{"idp_entity_id":"updated-eid"}`)
|
||||
req := httptest.NewRequest(http.MethodPut, "/api/v1/accounts/abc/saml_settings/1", body)
|
||||
req := httptest.NewRequest(http.MethodPut, "/api/v1/accounts/abc/saml_settings", body)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
s.router.ServeHTTP(w, req)
|
||||
s.Equal(http.StatusBadRequest, w.Code)
|
||||
@@ -131,6 +192,11 @@ func (s *AccountSamlSettingsHandlerTestSuite) TestUpdate_Success() {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
s.router.ServeHTTP(w, req)
|
||||
s.Equal(http.StatusOK, w.Code)
|
||||
|
||||
var payload map[string]interface{}
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &payload))
|
||||
s.Equal("updated-eid", payload["idp_entity_id"])
|
||||
s.NotContains(payload, "data")
|
||||
}
|
||||
|
||||
func (s *AccountSamlSettingsHandlerTestSuite) TestUpdate_NotFound() {
|
||||
@@ -145,7 +211,7 @@ func (s *AccountSamlSettingsHandlerTestSuite) TestUpdate_NotFound() {
|
||||
|
||||
func (s *AccountSamlSettingsHandlerTestSuite) TestDelete_InvalidAccountID() {
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodDelete, "/api/v1/accounts/abc/saml_settings/1", nil)
|
||||
req := httptest.NewRequest(http.MethodDelete, "/api/v1/accounts/abc/saml_settings", nil)
|
||||
s.router.ServeHTTP(w, req)
|
||||
s.Equal(http.StatusBadRequest, w.Code)
|
||||
}
|
||||
@@ -189,4 +255,4 @@ func (s *AccountSamlSettingsHandlerTestSuite) TestToggleActive_Success() {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
s.router.ServeHTTP(w, req)
|
||||
s.Equal(http.StatusOK, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user