From 1930b86e36cccf5e6b81fba1258bad9632d1b8ba Mon Sep 17 00:00:00 2001 From: Logan Date: Mon, 26 Aug 2024 18:09:14 +0100 Subject: [PATCH] test(getShortURL): base test for HTTP get on a short URL (#22) * test(getShortURL): base test for HTTP get on a short URL --- docker-compose-test.yaml | 6 ++- handlers_test.go | 97 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 3 deletions(-) diff --git a/docker-compose-test.yaml b/docker-compose-test.yaml index 4edb287..e21dd1e 100644 --- a/docker-compose-test.yaml +++ b/docker-compose-test.yaml @@ -16,12 +16,14 @@ services: volumes: - pgdatatest:/var/lib/postgresql/data - ./bin/local-init.sql:/docker-entrypoint-initdb.d/local-init.sql - ports: - - 5002:5432 healthcheck: test: ["CMD-SHELL", "pg_isready"] interval: 2s timeout: 5s retries: 10 + redis: + image: 'bitnami/redis:latest' + environment: + - ALLOW_EMPTY_PASSWORD=yes volumes: pgdatatest: diff --git a/handlers_test.go b/handlers_test.go index 7371069..b7684bb 100644 --- a/handlers_test.go +++ b/handlers_test.go @@ -13,6 +13,7 @@ import ( "testing" "github.com/pressly/goose/v3" + "github.com/redis/go-redis/v9" "golang.org/x/crypto/bcrypt" "url-short/internal/database" @@ -25,7 +26,7 @@ var ( userBadInput = []byte(`{"gmail":"test@mail.com", "auth": "test", "extra_data": "data"}`) userBadEmail = []byte(`{"email": "test1mail.com", "password": "test"}`) - longUrl = []byte(`{"long_url":"www.google.com"}`) + longUrl = []byte(`{"long_url":"https://www.google.com"}`) ) func resetDB(db *sql.DB) error { @@ -568,3 +569,97 @@ func TestPostLongURL(t *testing.T) { } }) } + +func TestGetShortURL(t *testing.T) { + dbURL := os.Getenv("PG_CONN") + db, err := sql.Open("postgres", dbURL) + rdbURL := os.Getenv("RDB_CONN") + + if err != nil { + t.Errorf("can not open database connection") + } + + defer db.Close() + + err = resetDB(db) + + if err != nil { + t.Errorf("could not resetDB %q", err) + } + + dbQueries := database.New(db) + + opt, err := redis.ParseURL(rdbURL) + + if err != nil { + t.Errorf("oculd not parse reddis connection") + } + + redisClient := redis.NewClient(opt) + + defer redisClient.Close() + + apiCfg := apiConfig{ + DB: dbQueries, + RDB: redisClient, + } + + _, err = setupUserOne(&apiCfg) + + if err != nil { + t.Errorf("can not set up user for test case with err %q", err) + } + + userOne, err := loginUserOne(&apiCfg) + + if err != nil { + t.Errorf("can not login user one for test case with err %q", err) + } + + postLongURLRequest := httptest.NewRequest( + http.MethodPost, + "/api/v1/data/shorten", + bytes.NewBuffer(longUrl), + ) + + buildHeader := fmt.Sprintf("Bearer %s", userOne.RefreshToken) + postLongURLRequest.Header.Set("Authorization", buildHeader) + + postURLResponse := httptest.NewRecorder() + + user, err := dbQueries.SelectUser(postLongURLRequest.Context(), userOne.Email) + + if err != nil { + t.Error("could not find user that was expected to exist") + } + + apiCfg.postLongURL(postURLResponse, postLongURLRequest, user) + + gotPutLongURL := LongURLResponse{} + + _ = json.NewDecoder(postURLResponse.Body).Decode(&gotPutLongURL) + + t.Run("test short url redirects to a long URL", func(t *testing.T) { + getShortURLRequest := httptest.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/v1/%s", gotPutLongURL.ShortURL), + http.NoBody, + ) + + getShortURLRequest.SetPathValue("shortUrl", gotPutLongURL.ShortURL) + + getShortURLResponse := httptest.NewRecorder() + + apiCfg.getShortURL(getShortURLResponse, getShortURLRequest) + + redirectLocation := getShortURLResponse.Result().Header.Get("Location") + + if redirectLocation != "https://www.google.com" { + t.Errorf( + "incorrect redirect to longURL got %q wanted %q", + redirectLocation, + "https://www.google.com", + ) + } + }) +}