Skip to content

Commit

Permalink
test: short url can be created via a postLongURL (#21)
Browse files Browse the repository at this point in the history
* test: short url can be created via a postLongURL
  • Loading branch information
logan-bobo authored Aug 22, 2024
1 parent 660d090 commit 23759dc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RUN apk upgrade

FROM base AS builder

RUN apk add --update go=1.22.5-r0
RUN apk add --update go=1.22.6-r0

WORKDIR /build

Expand All @@ -15,7 +15,7 @@ RUN go build -o main .

FROM base AS tester

RUN apk add --update go=1.22.5-r0
RUN apk add --update go=1.22.6-r0

RUN wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.59.1

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module url-short

go 1.22.4
go 1.22.6

require (
github.com/golang-jwt/jwt/v5 v5.2.1
Expand Down
79 changes: 77 additions & 2 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var (
userOneBadPassword = []byte(`{"email": "[email protected]", "password": "testerrrrr"}`)
userBadInput = []byte(`{"gmail":"[email protected]", "auth": "test", "extra_data": "data"}`)
userBadEmail = []byte(`{"email": "test1mail.com", "password": "test"}`)

longUrl = []byte(`{"long_url":"www.google.com"}`)
)

func resetDB(db *sql.DB) error {
Expand Down Expand Up @@ -460,7 +462,7 @@ func TestPutUser(t *testing.T) {

putUserResponse := httptest.NewRecorder()

user, err := dbQueries.SelectUser(putUserRequest.Context(), "[email protected]")
user, err := dbQueries.SelectUser(putUserRequest.Context(), userOne.Email)

if err != nil {
t.Error("could not find user that was expected to exist")
Expand All @@ -480,7 +482,7 @@ func TestPutUser(t *testing.T) {
t.Errorf("did not get expected email and ID on post user request")
}

userPostUpdate, err := dbQueries.SelectUser(putUserRequest.Context(), "[email protected]")
userPostUpdate, err := dbQueries.SelectUser(putUserRequest.Context(), userOne.Email)

if err != nil {
t.Error("could not get user post password change")
Expand All @@ -493,3 +495,76 @@ func TestPutUser(t *testing.T) {
}
})
}

func TestPostLongURL(t *testing.T) {
dbURL := os.Getenv("PG_CONN")
db, err := sql.Open("postgres", dbURL)

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)

apiCfg := apiConfig{
DB: dbQueries,
}

_, 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)
}

t.Run("test user can create short URL based on long", func(t *testing.T) {
postLongURLRequest := httptest.NewRequest(http.MethodPost, "/api/v1/data/shorten", bytes.NewBuffer(longUrl))

buildHeader := fmt.Sprintf("Bearer %s", userOne.RefreshToken)
postLongURLRequest.Header.Set("Authorization", buildHeader)

response := httptest.NewRecorder()

user, err := dbQueries.SelectUser(postLongURLRequest.Context(), userOne.Email)

if err != nil {
t.Error("could not find user that was expected to exist")
}

// As there are no hashes in the database there is no chance of a collision
// the first hash we generate here and the hash in the call to the handler will
// always be the same so we compare the two
hash, err := hashCollisionDetection(apiCfg.DB, "www.google.com", 1, postLongURLRequest.Context())

if err != nil {
t.Errorf("could not generate hash err %q", err)
}

apiCfg.postLongURL(response, postLongURLRequest, user)

gotPutLongURL := LongURLResponse{}

err = json.NewDecoder(response.Body).Decode(&gotPutLongURL)

if err != nil {
t.Errorf("could not decode request err %q", err)
}

if gotPutLongURL.ShortURL == hash {
t.Errorf("hash did not match")
}
})
}

0 comments on commit 23759dc

Please sign in to comment.