|
| 1 | +package client_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/drone/ff-golang-server-sdk/client" |
| 10 | + "github.com/drone/ff-golang-server-sdk/dto" |
| 11 | + "github.com/drone/ff-golang-server-sdk/evaluation" |
| 12 | + "github.com/drone/ff-golang-server-sdk/rest" |
| 13 | + "github.com/jarcoal/httpmock" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + sdkKey = "27bed8d2-2610-462b-90eb-d80fd594b623" |
| 19 | + URL = "http://localhost/api/1.0" |
| 20 | + //nolint |
| 21 | + AuthToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwcm9qZWN0IjoiMTA0MjM5NzYtODQ1MS00NmZjLTg2NzctYmNiZDM3MTA3M2JhIiwiZW52aXJvbm1lbnQiOiI3ZWQxMDI1ZC1hOWIxLTQxMjktYTg4Zi1lMjdlZjM2MDk4MmQiLCJwcm9qZWN0SWRlbnRpZmllciI6IiIsImVudmlyb25tZW50SWRlbnRpZmllciI6IlByZVByb2R1Y3Rpb24iLCJhY2NvdW50SUQiOiIiLCJvcmdhbml6YXRpb24iOiIwMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAifQ.z6EYSDVWwwAY6OTc2PnjSub43R6lOSJywlEObi6PDqQ" |
| 22 | +) |
| 23 | + |
| 24 | +// TestMain runs before the other tests |
| 25 | +func TestMain(m *testing.M) { |
| 26 | + // httpMock overwrites the http.DefaultClient |
| 27 | + httpmock.Activate() |
| 28 | + defer httpmock.DeactivateAndReset() |
| 29 | + |
| 30 | + // Register Default Responders |
| 31 | + httpmock.RegisterResponder("POST", "http://localhost/api/1.0/client/auth", ValidAuthResponse) |
| 32 | + httpmock.RegisterResponder("GET", "http://localhost/api/1.0/client/env/7ed1025d-a9b1-4129-a88f-e27ef360982d/target-segments", TargetSegmentsResponse) |
| 33 | + httpmock.RegisterResponder("GET", "http://localhost/api/1.0/client/env/7ed1025d-a9b1-4129-a88f-e27ef360982d/feature-configs", FeatureConfigsResponse) |
| 34 | + |
| 35 | + os.Exit(m.Run()) |
| 36 | +} |
| 37 | + |
| 38 | +func TestCfClient_BoolVariation(t *testing.T) { |
| 39 | + |
| 40 | + client, target, err := MakeNewClientAndTarget() |
| 41 | + if err != nil { |
| 42 | + t.Error(err) |
| 43 | + } |
| 44 | + |
| 45 | + type args struct { |
| 46 | + key string |
| 47 | + target *evaluation.Target |
| 48 | + defaultValue bool |
| 49 | + } |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + args args |
| 53 | + want bool |
| 54 | + wantErr bool |
| 55 | + }{ |
| 56 | + {"Test Invalid Flag Name returns default value", args{"MadeUpIDontExist", target, false}, false, false}, |
| 57 | + {"Test Default True Flag when On returns true", args{"TestTrueOn", target, false}, true, false}, |
| 58 | + {"Test Default True Flag when Off returns false", args{"TestTrueOff", target, true}, false, false}, |
| 59 | + {"Test Default False Flag when On returns false", args{"TestTrueOn", target, false}, true, false}, |
| 60 | + {"Test Default False Flag when Off returns true", args{"TestTrueOff", target, true}, false, false}, |
| 61 | + {"Test Default True Flag when Pre-Req is False returns false", args{"TestTrueOnWithPreReqFalse", target, true}, false, false}, |
| 62 | + {"Test Default True Flag when Pre-Req is True returns true", args{"TestTrueOnWithPreReqTrue", target, true}, true, false}, |
| 63 | + } |
| 64 | + for _, tt := range tests { |
| 65 | + test := tt |
| 66 | + t.Run(test.name, func(t *testing.T) { |
| 67 | + flag, err := client.BoolVariation(test.args.key, test.args.target, test.args.defaultValue) |
| 68 | + if (err != nil) != test.wantErr { |
| 69 | + t.Errorf("BoolVariation() error = %v, wantErr %v", err, test.wantErr) |
| 70 | + return |
| 71 | + } |
| 72 | + assert.Equal(t, test.want, flag, "%s didn't get expected value", test.name) |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestCfClient_StringVariation(t *testing.T) { |
| 78 | + |
| 79 | + client, target, err := MakeNewClientAndTarget() |
| 80 | + if err != nil { |
| 81 | + t.Error(err) |
| 82 | + } |
| 83 | + |
| 84 | + type args struct { |
| 85 | + key string |
| 86 | + target *evaluation.Target |
| 87 | + defaultValue string |
| 88 | + } |
| 89 | + tests := []struct { |
| 90 | + name string |
| 91 | + args args |
| 92 | + want string |
| 93 | + wantErr bool |
| 94 | + }{ |
| 95 | + {"Test Invalid Flag Name returns default value", args{"MadeUpIDontExist", target, "foo"}, "foo", false}, |
| 96 | + {"Test Default String Flag with when On returns A", args{"TestStringAOn", target, "foo"}, "A", false}, |
| 97 | + {"Test Default String Flag when Off returns B", args{"TestStringAOff", target, "foo"}, "B", false}, |
| 98 | + {"Test Default String Flag when Pre-Req is False returns B", args{"TestStringAOnWithPreReqFalse", target, "foo"}, "B", false}, |
| 99 | + {"Test Default String Flag when Pre-Req is True returns A", args{"TestStringAOnWithPreReqTrue", target, "foo"}, "A", false}, |
| 100 | + } |
| 101 | + for _, tt := range tests { |
| 102 | + test := tt |
| 103 | + t.Run(test.name, func(t *testing.T) { |
| 104 | + flag, err := client.StringVariation(test.args.key, test.args.target, test.args.defaultValue) |
| 105 | + if (err != nil) != test.wantErr { |
| 106 | + t.Errorf("BoolVariation() error = %v, wantErr %v", err, test.wantErr) |
| 107 | + return |
| 108 | + } |
| 109 | + assert.Equal(t, test.want, flag, "%s didn't get expected value", test.name) |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// MakeNewClientAndTarget creates a new client and target. If it returns |
| 115 | +// error then something went wrong. |
| 116 | +func MakeNewClientAndTarget() (*client.CfClient, *evaluation.Target, error) { |
| 117 | + target := target() |
| 118 | + client, err := newClient(http.DefaultClient) |
| 119 | + if err != nil { |
| 120 | + return nil, nil, err |
| 121 | + } |
| 122 | + |
| 123 | + // Wait to be authenticated - we can timeout if the channel doesn't return |
| 124 | + if ok, err := client.IsInitialized(); !ok { |
| 125 | + return nil, nil, err |
| 126 | + } |
| 127 | + |
| 128 | + return client, target, nil |
| 129 | +} |
| 130 | + |
| 131 | +// newClient creates a new client with some default options |
| 132 | +func newClient(httpClient *http.Client) (*client.CfClient, error) { |
| 133 | + return client.NewCfClient(sdkKey, |
| 134 | + client.WithURL(URL), |
| 135 | + client.WithStreamEnabled(false), |
| 136 | + client.WithHTTPClient(httpClient), |
| 137 | + client.WithStoreEnabled(false), |
| 138 | + ) |
| 139 | +} |
| 140 | + |
| 141 | +// target creates a new Target with some default values |
| 142 | +func target() *evaluation.Target { |
| 143 | + target := dto.NewTargetBuilder("john"). |
| 144 | + Firstname("John"). |
| 145 | + Lastname("Doe"). |
| 146 | + |
| 147 | + Build() |
| 148 | + return target |
| 149 | +} |
| 150 | + |
| 151 | +var ValidAuthResponse = func(req *http.Request) (*http.Response, error) { |
| 152 | + return httpmock.NewJsonResponse(200, rest.AuthenticationResponse{ |
| 153 | + AuthToken: AuthToken}) |
| 154 | +} |
| 155 | + |
| 156 | +var TargetSegmentsResponse = func(req *http.Request) (*http.Response, error) { |
| 157 | + var AllSegmentsResponse []rest.Segment |
| 158 | + |
| 159 | + err := json.Unmarshal([]byte(`[ |
| 160 | + { |
| 161 | + "environment": "PreProduction", |
| 162 | + "excluded": [], |
| 163 | + "identifier": "Beta_Users", |
| 164 | + "included": [ |
| 165 | + { |
| 166 | + "identifier": "john", |
| 167 | + "name": "John", |
| 168 | + }, |
| 169 | + { |
| 170 | + "identifier": "paul", |
| 171 | + "name": "Paul", |
| 172 | + } |
| 173 | + ], |
| 174 | + "name": "Beta Users" |
| 175 | + } |
| 176 | + ]`), &AllSegmentsResponse) |
| 177 | + if err != nil { |
| 178 | + return jsonError(err) |
| 179 | + } |
| 180 | + return httpmock.NewJsonResponse(200, AllSegmentsResponse) |
| 181 | +} |
| 182 | + |
| 183 | +var FeatureConfigsResponse = func(req *http.Request) (*http.Response, error) { |
| 184 | + var FeatureConfigResponse []rest.FeatureConfig |
| 185 | + FeatureConfigResponse = append(FeatureConfigResponse, MakeBoolFeatureConfigs("TestTrueOn", "true", "false", "on")...) |
| 186 | + FeatureConfigResponse = append(FeatureConfigResponse, MakeBoolFeatureConfigs("TestTrueOff", "true", "false", "off")...) |
| 187 | + |
| 188 | + FeatureConfigResponse = append(FeatureConfigResponse, MakeBoolFeatureConfigs("TestFalseOn", "false", "true", "on")...) |
| 189 | + FeatureConfigResponse = append(FeatureConfigResponse, MakeBoolFeatureConfigs("TestFalseOff", "false", "true", "off")...) |
| 190 | + |
| 191 | + FeatureConfigResponse = append(FeatureConfigResponse, MakeBoolFeatureConfigs("TestTrueOnWithPreReqFalse", "true", "false", "on", MakeBoolPreRequisite("PreReq1", "false"))...) |
| 192 | + FeatureConfigResponse = append(FeatureConfigResponse, MakeBoolFeatureConfigs("TestTrueOnWithPreReqTrue", "true", "false", "on", MakeBoolPreRequisite("PreReq1", "true"))...) |
| 193 | + |
| 194 | + FeatureConfigResponse = append(FeatureConfigResponse, MakeStringFeatureConfigs("TestStringAOn", "Alpha", "Bravo", "on")...) |
| 195 | + FeatureConfigResponse = append(FeatureConfigResponse, MakeStringFeatureConfigs("TestStringAOff", "Alpha", "Bravo", "off")...) |
| 196 | + |
| 197 | + FeatureConfigResponse = append(FeatureConfigResponse, MakeStringFeatureConfigs("TestStringAOnWithPreReqFalse", "Alpha", "Bravo", "on", MakeBoolPreRequisite("PreReq1", "false"))...) |
| 198 | + FeatureConfigResponse = append(FeatureConfigResponse, MakeStringFeatureConfigs("TestStringAOnWithPreReqTrue", "Alpha", "Bravo", "on", MakeBoolPreRequisite("PreReq1", "true"))...) |
| 199 | + |
| 200 | + return httpmock.NewJsonResponse(200, FeatureConfigResponse) |
| 201 | +} |
0 commit comments