|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/go-feature-flag/app-api/dao" |
| 7 | + daoErr "github.com/go-feature-flag/app-api/dao/err" |
| 8 | + "github.com/go-feature-flag/app-api/model" |
| 9 | + "github.com/go-feature-flag/app-api/testutils" |
| 10 | + "github.com/labstack/echo/v4" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "net/http" |
| 14 | + "net/http/httptest" |
| 15 | + "testing" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +func TestFlagsHandler_GetAllFeatureFlags(t *testing.T) { |
| 20 | + type test struct { |
| 21 | + name string |
| 22 | + ctx context.Context |
| 23 | + flags []model.FeatureFlag |
| 24 | + expectedHTTPCode int |
| 25 | + expectedBody string |
| 26 | + } |
| 27 | + tests := []test{ |
| 28 | + { |
| 29 | + name: "should return an empty array of flags if there are no flags", |
| 30 | + ctx: context.Background(), |
| 31 | + expectedHTTPCode: http.StatusOK, |
| 32 | + flags: make([]model.FeatureFlag, 0), |
| 33 | + expectedBody: "[]\n", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "should return a flag with a default rule", |
| 37 | + ctx: context.Background(), |
| 38 | + expectedHTTPCode: http.StatusOK, |
| 39 | + flags: []model.FeatureFlag{ |
| 40 | + { |
| 41 | + ID: "926214f3-80c1-46e6-a913-b2d40b92a932", |
| 42 | + Name: "flag1", |
| 43 | + Description: testutils.String("description1"), |
| 44 | + Variations: &map[string]*interface{}{ |
| 45 | + "variation1": testutils.Interface("A"), |
| 46 | + "variation2": testutils.Interface("B"), |
| 47 | + }, |
| 48 | + LastModifiedBy: "foo", |
| 49 | + LastUpdatedDate: time.Unix(1729849827, 0), |
| 50 | + CreatedDate: time.Unix(1729849827, 0), |
| 51 | + DefaultRule: &model.Rule{ |
| 52 | + VariationResult: testutils.String("variation1"), |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + expectedBody: "[{\"id\":\"926214f3-80c1-46e6-a913-b2d40b92a932\",\"name\":\"flag1\",\"createdDate\":\"2024-10-25T11:50:27+02:00\",\"lastUpdatedDate\":\"2024-10-25T11:50:27+02:00\",\"LastModifiedBy\":\"foo\",\"description\":\"description1\",\"type\":\"\",\"variations\":{\"variation1\":\"A\",\"variation2\":\"B\"},\"defaultRule\":{\"id\":\"\",\"variation\":\"variation1\"}}]\n", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "should return a 500 if an error occured ", |
| 60 | + ctx: context.WithValue(context.Background(), "error", daoErr.UnknownError), |
| 61 | + expectedHTTPCode: http.StatusInternalServerError, |
| 62 | + flags: make([]model.FeatureFlag, 0), |
| 63 | + expectedBody: "{\"errorDetails\":\"error on get flags\",\"code\":500}\n", |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + t.Run(tt.name, func(t *testing.T) { |
| 69 | + e := echo.New() |
| 70 | + mockDao, err := dao.NewInMemoryMockDao() |
| 71 | + require.NoError(t, err) |
| 72 | + mockDao.SetFlags(tt.flags) |
| 73 | + |
| 74 | + h := NewFlagAPIHandler(mockDao) |
| 75 | + req := httptest.NewRequestWithContext(tt.ctx, http.MethodGet, "/v1/flags", nil) |
| 76 | + rec := httptest.NewRecorder() |
| 77 | + c := e.NewContext(req, rec) |
| 78 | + |
| 79 | + require.NoError(t, h.GetAllFeatureFlags(c)) |
| 80 | + assert.Equal(t, tt.expectedHTTPCode, rec.Code) |
| 81 | + fmt.Println(rec.Body.String()) |
| 82 | + assert.JSONEq(t, tt.expectedBody, rec.Body.String()) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
0 commit comments