Hackaton: Add more unit tests, take 2 (#101473)

* login/social/socialimpl: add assertions for usage stats, support bundle and oauthinfo methods

* accesscontrol/acimpl: add tests for GetRoleByName

* anonymous/sortopts: add tests for Sorter

* cloudmigration/gmsclient: add basic test cases for all methods

* shorturls/shorturlimpl: add more edge test cases

* tag/tagimpl: add test to cover duplicate tag kv and nil pairs

* updatechecker: add test cases for module
This commit is contained in:
Matheus Macabu
2025-03-03 13:12:13 +01:00
committed by GitHub
parent d1b20c652d
commit b110754c9a
10 changed files with 887 additions and 5 deletions
@@ -75,6 +75,8 @@ func TestShortURLService(t *testing.T) {
t.Run("and no action when no stale short urls exist", func(t *testing.T) {
cmd := shorturls.DeleteShortUrlCommand{OlderThan: time.Unix(existingShortURL.CreatedAt, 0)}
err = service.DeleteStaleShortURLs(context.Background(), &cmd)
require.NoError(t, err)
require.Equal(t, int64(0), cmd.NumDeleted)
})
@@ -89,4 +91,46 @@ func TestShortURLService(t *testing.T) {
require.True(t, shorturls.ErrShortURLNotFound.Is(err))
require.Nil(t, shortURL)
})
t.Run("User cannot create short URLs from invalid paths", func(t *testing.T) {
service := ShortURLService{SQLStore: &sqlStore{db: store}}
ctx := context.Background()
absolutePath := "/path?test=true"
newShortURL, err := service.CreateShortURL(ctx, user, absolutePath)
require.ErrorIs(t, err, shorturls.ErrShortURLAbsolutePath)
require.Nil(t, newShortURL)
relativePath := "path/../test?test=true"
newShortURL, err = service.CreateShortURL(ctx, user, relativePath)
require.ErrorIs(t, err, shorturls.ErrShortURLInvalidPath)
require.Nil(t, newShortURL)
relativePath = "../path/test?test=true"
newShortURL, err = service.CreateShortURL(ctx, user, relativePath)
require.ErrorIs(t, err, shorturls.ErrShortURLInvalidPath)
require.Nil(t, newShortURL)
})
t.Run("The same URL will generate different entries", func(t *testing.T) {
service := ShortURLService{SQLStore: &sqlStore{db: store}}
ctx := context.Background()
const refPath = "mock/path?test=true"
newShortURL1, err := service.CreateShortURL(ctx, user, refPath)
require.NoError(t, err)
require.NotNil(t, newShortURL1)
require.NotEmpty(t, newShortURL1.Uid)
newShortURL2, err := service.CreateShortURL(ctx, user, refPath)
require.NoError(t, err)
require.NotNil(t, newShortURL2)
require.NotEmpty(t, newShortURL2.Uid)
require.NotEqual(t, newShortURL1.Uid, newShortURL2.Uid)
require.Equal(t, newShortURL1.Path, newShortURL2.Path)
})
}