diff options
Diffstat (limited to 'api/cmd')
| -rw-r--r-- | api/cmd/main_test.go | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/api/cmd/main_test.go b/api/cmd/main_test.go index 8d0df8d..a6c8381 100644 --- a/api/cmd/main_test.go +++ b/api/cmd/main_test.go | |||
| @@ -12,8 +12,9 @@ import ( | |||
| 12 | ) | 12 | ) |
| 13 | 13 | ||
| 14 | func getTestUserCredentials() (string, string) { | 14 | func getTestUserCredentials() (string, string) { |
| 15 | viper.SetConfigName(".env") | 15 | viper.SetConfigFile("../.env") |
| 16 | viper.AddConfigPath(".") | 16 | viper.AddConfigPath(".") |
| 17 | viper.AutomaticEnv() | ||
| 17 | err := viper.ReadInConfig() | 18 | err := viper.ReadInConfig() |
| 18 | if err != nil { | 19 | if err != nil { |
| 19 | log.Fatalf("Error while reading config file %s", err) | 20 | log.Fatalf("Error while reading config file %s", err) |
| @@ -29,17 +30,29 @@ func TestAuthRoute(t *testing.T) { | |||
| 29 | 30 | ||
| 30 | username, password := getTestUserCredentials() | 31 | username, password := getTestUserCredentials() |
| 31 | 32 | ||
| 33 | log.Println("username", username) | ||
| 34 | log.Println("password", password) | ||
| 35 | |||
| 32 | w := httptest.NewRecorder() | 36 | w := httptest.NewRecorder() |
| 33 | req, _ := http.NewRequest("POST", "/api/v1/auth", nil) | 37 | req, err := http.NewRequest("POST", "/api/v1/auth", nil) |
| 38 | if err != nil { | ||
| 39 | t.Fatalf("Failed to create request: %v", err) | ||
| 40 | } | ||
| 34 | req.SetBasicAuth(username, password) | 41 | req.SetBasicAuth(username, password) |
| 35 | router.ServeHTTP(w, req) | 42 | router.ServeHTTP(w, req) |
| 36 | 43 | ||
| 37 | assert.Equal(t, http.StatusOK, w.Code, "response should return a 200 code") | 44 | assert.Equal(t, http.StatusOK, w.Code, "response should return a 200 code") |
| 38 | 45 | ||
| 39 | var response map[string]interface{} | 46 | var response map[string]interface{} |
| 40 | _ = json.Unmarshal(w.Body.Bytes(), &response) | 47 | err = json.Unmarshal(w.Body.Bytes(), &response) |
| 48 | if err != nil { | ||
| 49 | t.Fatalf("Failed to unmarshal response: %v", err) | ||
| 50 | } | ||
| 41 | _, exists := response["token"] | 51 | _, exists := response["token"] |
| 42 | assert.True(t, exists, "response should return a token") | 52 | assert.True(t, exists, "response should return a token") |
| 53 | if !exists { | ||
| 54 | t.Fatalf("response did not contain token") | ||
| 55 | } | ||
| 43 | } | 56 | } |
| 44 | 57 | ||
| 45 | func TestAuthRouteFailure(t *testing.T) { | 58 | func TestAuthRouteFailure(t *testing.T) { |
| @@ -52,5 +65,3 @@ func TestAuthRouteFailure(t *testing.T) { | |||
| 52 | 65 | ||
| 53 | assert.Equal(t, http.StatusUnauthorized, w.Code, "should return a 401 code") | 66 | assert.Equal(t, http.StatusUnauthorized, w.Code, "should return a 401 code") |
| 54 | } | 67 | } |
| 55 | |||
| 56 | func Test \ No newline at end of file | ||
