-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: short url can be created via a postLongURL (#21)
* test: short url can be created via a postLongURL
- Loading branch information
1 parent
660d090
commit 23759dc
Showing
3 changed files
with
80 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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") | ||
|
@@ -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") | ||
|
@@ -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") | ||
} | ||
}) | ||
} |