package main import ( "encoding/json" "log" "net/http" "net/http/httptest" "testing" "github.com/spf13/viper" "github.com/stretchr/testify/assert" ) func getTestUserCredentials() (string, string) { viper.SetConfigName(".env") viper.AddConfigPath(".") err := viper.ReadInConfig() if err != nil { log.Fatalf("Error while reading config file %s", err) } testUser := viper.GetString("TEST_USER") testPass := viper.GetString("TEST_PASS") return testUser, testPass } func TestAuthRoute(t *testing.T) { router := setupRouter() username, password := getTestUserCredentials() w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/auth", nil) req.SetBasicAuth(username, password) router.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code, "response should return a 200 code") var response map[string]interface{} _ = json.Unmarshal(w.Body.Bytes(), &response) _, exists := response["token"] assert.True(t, exists, "response should return a token") } func TestAuthRouteFailure(t *testing.T) { router := setupRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/auth", nil) req.SetBasicAuth("asdf", "asdf") router.ServeHTTP(w, req) assert.Equal(t, http.StatusUnauthorized, w.Code, "should return a 401 code") } func Test