Skip to content

Commit

Permalink
Fix user creation with special chars (#292)
Browse files Browse the repository at this point in the history
* Tests fixed

* Fix account user API with special chars

---------

Co-authored-by: Raphael Attal <[email protected]>
  • Loading branch information
Raphyo5 and attalraphael authored Feb 21, 2023
1 parent 832587a commit 25da2da
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
5 changes: 3 additions & 2 deletions incapsula/client_account_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"net/http"
"net/url"
)

// Endpoints (unexported consts)
Expand Down Expand Up @@ -119,7 +120,7 @@ func (c *Client) GetAccountUser(accountID int, email string) (*UserApisResponse,
log.Printf("[INFO] Getting Incapsula user status for email id: %s\n", email)

// Get to Incapsula
reqURL := fmt.Sprintf("%s/%s?caid=%d&email=%s", c.config.BaseURLAPI, endpointUserStatus, accountID, email)
reqURL := fmt.Sprintf("%s/%s?caid=%d&email=%s", c.config.BaseURLAPI, endpointUserStatus, accountID, url.QueryEscape(email))
resp, err := c.DoJsonRequestWithHeaders(http.MethodGet, reqURL, nil, ReadAccountUser)

if err != nil {
Expand Down Expand Up @@ -210,7 +211,7 @@ func (c *Client) DeleteAccountUser(accountID int, email string) error {

// Delete form to Incapsula

reqURL := fmt.Sprintf("%s/%s?caid=%d&email=%s", c.config.BaseURLAPI, endpointUserDelete, accountID, email)
reqURL := fmt.Sprintf("%s/%s?caid=%d&email=%s", c.config.BaseURLAPI, endpointUserDelete, accountID, url.QueryEscape(email))
resp, err := c.DoJsonRequestWithHeaders(http.MethodDelete, reqURL, nil, DeleteAccountUser)

if err != nil {
Expand Down
32 changes: 26 additions & 6 deletions incapsula/resource_account_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
const accountResourceUserType = "incapsula_account_user"
const accountResourceUserName = "test-terraform-account-user"
const accountResourceUserTypeName = accountResourceUserType + "." + accountResourceUserName
const accountUserEmail = "[email protected]"
const accountUserEmail = "[email protected]"
const accountUserEmailSpecialChar = "[email protected]"
const accountUserFirstName = "First"
const accountUserLastName = "Last"

Expand All @@ -24,7 +25,7 @@ func TestIncapsulaAccountUser_Basic(t *testing.T) {
CheckDestroy: testCheckIncapsulaAccountUserDestroy,
Steps: []resource.TestStep{
{
Config: testCheckIncapsulaAccountUserConfigBasic(t),
Config: testCheckIncapsulaAccountUserConfigBasic(t, accountUserEmail),
Check: resource.ComposeTestCheckFunc(
testCheckIncapsulaAccountUserExists(accountResourceUserTypeName),
resource.TestCheckResourceAttr(accountResourceUserTypeName, "email", accountUserEmail),
Expand All @@ -36,14 +37,33 @@ func TestIncapsulaAccountUser_Basic(t *testing.T) {
})
}

func TestIncapsulaAccountUser_BasicWithSpecialChar(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckIncapsulaAccountUserDestroy,
Steps: []resource.TestStep{
{
Config: testCheckIncapsulaAccountUserConfigBasic(t, accountUserEmailSpecialChar),
Check: resource.ComposeTestCheckFunc(
testCheckIncapsulaAccountUserExists(accountResourceUserTypeName),
resource.TestCheckResourceAttr(accountResourceUserTypeName, "email", accountUserEmailSpecialChar),
resource.TestCheckResourceAttr(accountResourceUserTypeName, "first_name", accountUserFirstName),
resource.TestCheckResourceAttr(accountResourceUserTypeName, "last_name", accountUserLastName),
),
},
},
})
}

func TestIncapsulaAccountUser_Update(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckIncapsulaAccountUserDestroy,
Steps: []resource.TestStep{
{
Config: testCheckIncapsulaAccountUserConfigBasic(t),
Config: testCheckIncapsulaAccountUserConfigBasic(t, accountUserEmail),
Check: resource.ComposeTestCheckFunc(
testCheckIncapsulaAccountUserExists(accountResourceUserTypeName),
resource.TestCheckResourceAttr(accountResourceUserTypeName, "email", accountUserEmail),
Expand All @@ -69,7 +89,7 @@ func TestIncapsulaAccountUser_ImportBasic(t *testing.T) {
CheckDestroy: testCheckIncapsulaAccountUserDestroy,
Steps: []resource.TestStep{
{
Config: testCheckIncapsulaAccountUserConfigBasic(t),
Config: testCheckIncapsulaAccountUserConfigBasic(t, accountUserEmail),
},
{
ResourceName: accountResourceUserTypeName,
Expand Down Expand Up @@ -186,7 +206,7 @@ func testCheckIncapsulaAccountUserExists(name string) resource.TestCheckFunc {
}
}

func testCheckIncapsulaAccountUserConfigBasic(t *testing.T) string {
func testCheckIncapsulaAccountUserConfigBasic(t *testing.T, email string) string {
return fmt.Sprintf(`
data "incapsula_account_data" "account_data" {}
Expand All @@ -196,7 +216,7 @@ func testCheckIncapsulaAccountUserConfigBasic(t *testing.T) string {
first_name = "%s"
last_name = "%s"
}`,
accountResourceUserType, accountResourceUserName, accountUserEmail, accountUserFirstName, accountUserLastName,
accountResourceUserType, accountResourceUserName, email, accountUserFirstName, accountUserLastName,
)
}

Expand Down

0 comments on commit 25da2da

Please sign in to comment.