Skip to content

Commit

Permalink
Merge pull request #12 from flagship-io/feature-FS3-608
Browse files Browse the repository at this point in the history
Feature fs3 608
  • Loading branch information
kjose authored Aug 17, 2022
2 parents 49a7d8c + 57f133e commit 0409d8d
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.45.2
version: v1.48
args: --skip-files .*_test.go
- uses: codecov/codecov-action@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
git.mills.io/prologic/bitcask v1.0.2
github.com/aws/aws-sdk-go v1.40.45
github.com/flagship-io/flagship-common v0.0.18-beta.1
github.com/flagship-io/flagship-proto v0.0.15
github.com/flagship-io/flagship-proto v0.0.16
github.com/go-kit/kit v0.12.0
github.com/go-redis/redis/v8 v8.11.4
github.com/sirupsen/logrus v1.8.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ github.com/flagship-io/flagship-common v0.0.18-beta.1 h1:CBo4xCpGG4ui77GhGcOmejA
github.com/flagship-io/flagship-common v0.0.18-beta.1/go.mod h1:9NsiiubOPN6ZD8T4VvrSSwOWTloHTkoWftvRnmr4W7Y=
github.com/flagship-io/flagship-proto v0.0.15 h1:2sK9DWtnTkUEBiGtjLdwhMaaGsXuU+QRmAOhxlwj7bQ=
github.com/flagship-io/flagship-proto v0.0.15/go.mod h1:Nv5epf8wbbAEx6sAnjPumFy+YQOClXyXlxZzUMUqidw=
github.com/flagship-io/flagship-proto v0.0.16-0.20220816134540-8b1aa2ad5a0a h1:4FmZvUtZ2KjDD7V7nSJFodiAH7RYKUnMd0EQvHCFEYU=
github.com/flagship-io/flagship-proto v0.0.16-0.20220816134540-8b1aa2ad5a0a/go.mod h1:Nv5epf8wbbAEx6sAnjPumFy+YQOClXyXlxZzUMUqidw=
github.com/flagship-io/flagship-proto v0.0.16 h1:Mz/ljPweSR44Ek0moW5/yHPDz5DZO4fPTp+zHzC9nzQ=
github.com/flagship-io/flagship-proto v0.0.16/go.mod h1:Nv5epf8wbbAEx6sAnjPumFy+YQOClXyXlxZzUMUqidw=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/franela/goblin v0.0.0-20210519012713-85d372ac71e2/go.mod h1:VzmDKDJVZI3aJmnRI9VjAn9nJ8qPPsN1fqzr9dqInIo=
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20=
Expand Down
3 changes: 2 additions & 1 deletion internal/apilogic/handle_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func BuildHandleRequest(req *http.Request) (*handle.Request, error) {
handleRequest.ExposeAllKeys = exposeAllKeys == "true"
}

handleRequest.SendContextEvent = req.URL.Query().Get("sendContextEvent") != "false"
hasVisitorConsented := decisionRequest.VisitorConsent == nil || decisionRequest.VisitorConsent.GetValue()
handleRequest.SendContextEvent = req.URL.Query().Get("sendContextEvent") != "false" && hasVisitorConsented
handleRequest.DecisionRequest = decisionRequest
handleRequest.FullVisitorContext = &targeting.Context{
Standard: decisionRequest.GetContext(),
Expand Down
53 changes: 53 additions & 0 deletions internal/apilogic/handle_request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package apilogic

import (
"io"
"net/http"
"net/url"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBuildHandleRequestHasCorrectSendContextEvent(t *testing.T) {
tests := map[string]struct {
queryVisitorConsent string
bodyVisitorConsent string
result bool
}{
"VisitorConsentEmpty": {"", "", true},
"BodyVisitorConsentFalse": {"\"visitor_consent\": false,", "", false},
"BodyVisitorConsentTrue": {"\"visitor_consent\": true,", "", true},
"QueryVisitorConsentFalse": {"", "sendContextEvent=false", false},
"QueryVisitorConsentTrue": {"", "sendContextEvent=true", true},
"VisitorConsentBothTrue": {"\"visitor_consent\": true,", "sendContextEvent=true", true},
"VisitorConsentBothFalse": {"\"visitor_consent\": false,", "sendContextEvent=false", false},
"VisitorConsentBothDifferent": {"\"visitor_consent\": true,", "sendContextEvent=false", false},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
body := `{
"visitor_id": "123",
` + test.queryVisitorConsent + `
"context": {}
}`

req := &http.Request{
URL: &url.URL{
RawQuery: test.bodyVisitorConsent,
},
Body: io.NopCloser(strings.NewReader(body)),
Method: "POST",
}

hr, err := BuildHandleRequest(req)

assert.NotNil(t, hr)
assert.Nil(t, err)
assert.Equal(t, test.result, hr.SendContextEvent)
})
}

}
4 changes: 2 additions & 2 deletions pkg/connectors/environment_loaders/cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package environment_loaders

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -116,7 +116,7 @@ func (l *CDNLoader) fetchEnvironment(envID string, APIKey string) error {
return nil
}

response, err := ioutil.ReadAll(resp.Body)
response, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error when reading body: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/connectors/hits_processors/datacollect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package hits_processors

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"sync"
Expand Down Expand Up @@ -36,7 +36,7 @@ func TestDataCollectTrack(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// Send response to be tested
lock.Lock()
lastBodySent, _ := ioutil.ReadAll(req.Body)
lastBodySent, _ := io.ReadAll(req.Body)
bodySents = append(bodySents, string(lastBodySent))
_, err := rw.Write([]byte("{}"))
assert.Nil(t, err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/handlers/activate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -33,7 +32,7 @@ func TestActivate(t *testing.T) {
Activate(context)(w, req)

resp := w.Result()
bodyResp, _ := ioutil.ReadAll(resp.Body)
bodyResp, _ := io.ReadAll(resp.Body)
assert.Equal(t, 400, resp.StatusCode)
assert.Contains(t, string(bodyResp), "unknown field")

Expand All @@ -50,7 +49,7 @@ func TestActivate(t *testing.T) {
Activate(context)(w, req)

resp = w.Result()
bodyResp, _ = ioutil.ReadAll(resp.Body)
bodyResp, _ = io.ReadAll(resp.Body)
assert.Equal(t, 400, resp.StatusCode)
assert.Contains(t, string(bodyResp), "Field is mandatory")

Expand Down
10 changes: 5 additions & 5 deletions pkg/handlers/campaign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestSendSingleFormatResponse(t *testing.T) {
resp := w.Result()
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
assert.Contains(t, string(body), `"variation":`)

// other modification type returns json format
Expand All @@ -104,7 +104,7 @@ func TestSendSingleFormatResponse(t *testing.T) {
resp = w.Result()
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
body, _ = ioutil.ReadAll(resp.Body)
body, _ = io.ReadAll(resp.Body)
assert.Contains(t, string(body), `"variation":`)

// html type with single fields returns text/html with html value
Expand All @@ -117,7 +117,7 @@ func TestSendSingleFormatResponse(t *testing.T) {
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "text/html", resp.Header.Get("Content-Type"))

body, _ = ioutil.ReadAll(resp.Body)
body, _ = io.ReadAll(resp.Body)
assert.Equal(t, "value", string(body))

// text type with single fields returns text/plain withs stringified bool value
Expand All @@ -130,7 +130,7 @@ func TestSendSingleFormatResponse(t *testing.T) {
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "text/plain", resp.Header.Get("Content-Type"))

body, _ = ioutil.ReadAll(resp.Body)
body, _ = io.ReadAll(resp.Body)
assert.Equal(t, "true", string(body))

// text type with single fields returns text/plain withs stringified number value
Expand All @@ -139,6 +139,6 @@ func TestSendSingleFormatResponse(t *testing.T) {
sendSingleFormatResponse(w, campaign, logger.New("debug", "test"))

resp = w.Result()
body, _ = ioutil.ReadAll(resp.Body)
body, _ = io.ReadAll(resp.Body)
assert.Equal(t, "20.5", string(body))
}

0 comments on commit 0409d8d

Please sign in to comment.