From 33e9b01ccea8d46630f9d3d457951a7d914ce70c Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Sat, 11 Nov 2017 02:15:02 +0100 Subject: [PATCH] Fixed unit tests and cleaned up createEntry --- handlers/public.go | 7 ++++++- store/store.go | 6 +++--- store/store_test.go | 6 +++--- store/util.go | 11 +++-------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/handlers/public.go b/handlers/public.go index d5d3054..8e53a3a 100644 --- a/handlers/public.go +++ b/handlers/public.go @@ -65,7 +65,12 @@ func (h *Handler) handleCreate(c *gin.Context) { return } user := c.MustGet("user").(*jwtClaims) - id, err := h.store.CreateEntry(data.URL, c.ClientIP(), user.OAuthProvider, user.OAuthID) + id, err := h.store.CreateEntry(store.Entry{ + URL: data.URL, + RemoteAddr: c.ClientIP(), + OAuthProvider: user.OAuthProvider, + OAuthID: user.OAuthID, + }) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return diff --git a/store/store.go b/store/store.go index 2d77750..9c32c4e 100644 --- a/store/store.go +++ b/store/store.go @@ -114,13 +114,13 @@ func (s *Store) GetEntryByIDRaw(id string) ([]byte, error) { } // CreateEntry creates a new record and returns his short id -func (s *Store) CreateEntry(URL, remoteAddr, oAuthProvider, oAuthID string) (string, error) { - if !govalidator.IsURL(URL) { +func (s *Store) CreateEntry(entry Entry) (string, error) { + if !govalidator.IsURL(entry.URL) { return "", ErrNoValidURL } // try it 10 times to make a short URL for i := 1; i <= 10; i++ { - id, err := s.createEntry(URL, remoteAddr, oAuthProvider, oAuthID) + id, err := s.createEntry(entry) if err != nil { s.log.Debugf("Could not create entry: %v", err) continue diff --git a/store/store_test.go b/store/store_test.go index 6f9e42d..4cb4381 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -69,12 +69,12 @@ func TestCreateEntry(t *testing.T) { t.Fatalf("unexpected error: %v", err) } defer cleanup(store) - _, err = store.CreateEntry("", "") + _, err = store.CreateEntry(Entry{}) if err != ErrNoValidURL { t.Fatalf("unexpected error: %v", err) } for i := 1; i <= 100; i++ { - _, err := store.CreateEntry("https://golang.org/", "") + _, err := store.CreateEntry(Entry{URL: "https://golang.org/"}) if err != nil && err != ErrGeneratingTriesFailed { t.Fatalf("unexpected error during creating entry: %v", err) } @@ -103,7 +103,7 @@ func TestIncreaseVisitCounter(t *testing.T) { t.Fatalf("could not create store: %v", err) } defer cleanup(store) - id, err := store.CreateEntry("https://golang.org/", "") + id, err := store.CreateEntry(Entry{URL: "https://golang.org/"}) if err != nil { t.Fatalf("could not create entry: %v", err) } diff --git a/store/util.go b/store/util.go index cc4bfa8..69414e9 100644 --- a/store/util.go +++ b/store/util.go @@ -41,20 +41,15 @@ func (s *Store) createEntryRaw(key, value []byte) error { } // createEntry creates a new entry -func (s *Store) createEntry(URL, remoteAddr, oAuthProvider, oAuthID string) (string, error) { +func (s *Store) createEntry(entry Entry) (string, error) { id, err := generateRandomString(s.idLength) if err != nil { return "", errors.Wrap(err, "could not generate random string") } exists := s.checkExistence(id) if !exists { - raw, err := json.Marshal(Entry{ - URL: URL, - RemoteAddr: remoteAddr, - CreatedOn: time.Now(), - OAuthProvider: oAuthProvider, - OAuthID: oAuthID, - }) + entry.CreatedOn = time.Now() + raw, err := json.Marshal(entry) if err != nil { return "", err }