aboutsummaryrefslogtreecommitdiff
path: root/internal/db/users_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/db/users_test.go')
-rw-r--r--internal/db/users_test.go30
1 files changed, 29 insertions, 1 deletions
diff --git a/internal/db/users_test.go b/internal/db/users_test.go
index 4cf5aaf4..6a63c5aa 100644
--- a/internal/db/users_test.go
+++ b/internal/db/users_test.go
@@ -82,7 +82,7 @@ func TestUsers(t *testing.T) {
}
t.Parallel()
- tables := []interface{}{new(User), new(EmailAddress), new(Repository), new(Follow), new(PullRequest)}
+ tables := []interface{}{new(User), new(EmailAddress), new(Repository), new(Follow), new(PullRequest), new(PublicKey)}
db := &users{
DB: dbtest.NewDB(t, "users", tables...),
}
@@ -99,6 +99,7 @@ func TestUsers(t *testing.T) {
{"GetByEmail", usersGetByEmail},
{"GetByID", usersGetByID},
{"GetByUsername", usersGetByUsername},
+ {"GetByKeyID", usersGetByKeyID},
{"HasForkedRepository", usersHasForkedRepository},
{"IsUsernameUsed", usersIsUsernameUsed},
{"List", usersList},
@@ -553,6 +554,33 @@ func usersGetByUsername(t *testing.T, db *users) {
assert.Equal(t, wantErr, err)
}
+func usersGetByKeyID(t *testing.T, db *users) {
+ ctx := context.Background()
+
+ alice, err := db.Create(ctx, "alice", "alice@exmaple.com", CreateUserOptions{})
+ require.NoError(t, err)
+
+ // TODO: Use PublicKeys.Create to replace SQL hack when the method is available.
+ publicKey := &PublicKey{
+ OwnerID: alice.ID,
+ Name: "test-key",
+ Fingerprint: "12:f8:7e:78:61:b4:bf:e2:de:24:15:96:4e:d4:72:53",
+ Content: "test-key-content",
+ CreatedUnix: db.NowFunc().Unix(),
+ UpdatedUnix: db.NowFunc().Unix(),
+ }
+ err = db.WithContext(ctx).Create(publicKey).Error
+ require.NoError(t, err)
+
+ user, err := db.GetByKeyID(ctx, publicKey.ID)
+ require.NoError(t, err)
+ assert.Equal(t, alice.Name, user.Name)
+
+ _, err = db.GetByKeyID(ctx, publicKey.ID+1)
+ wantErr := ErrUserNotExist{args: errutil.Args{"keyID": publicKey.ID + 1}}
+ assert.Equal(t, wantErr, err)
+}
+
func usersHasForkedRepository(t *testing.T, db *users) {
ctx := context.Background()