Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Write tests
  • Loading branch information
ammario committed Sep 24, 2022
commit a89dee94c9b0982c33a77b71030f4b137d6386c1
16 changes: 16 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,22 @@ func (q *fakeQuerier) UpdateUserStatus(_ context.Context, arg database.UpdateUse
return database.User{}, sql.ErrNoRows
}

func (q *fakeQuerier) UpdateUserLastSeenAt(_ context.Context, arg database.UpdateUserLastSeenAtParams) (database.User, error) {
q.mutex.Lock()
defer q.mutex.Unlock()

for index, user := range q.users {
if user.ID != arg.ID {
continue
}
user.LastSeenAt = arg.LastSeenAt
user.UpdatedAt = arg.UpdatedAt
q.users[index] = user
return user, nil
}
return database.User{}, sql.ErrNoRows
}

func (q *fakeQuerier) UpdateUserHashedPassword(_ context.Context, arg database.UpdateUserHashedPasswordParams) error {
q.mutex.Lock()
defer q.mutex.Unlock()
Expand Down
13 changes: 13 additions & 0 deletions coderd/httpmw/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,19 @@ func ExtractAPIKey(cfg ExtractAPIKeyConfig) func(http.Handler) http.Handler {
return
}
}

_, err = cfg.DB.UpdateUserLastSeenAt(ctx, database.UpdateUserLastSeenAtParams{
ID: key.UserID,
LastSeenAt: database.Now(),
UpdatedAt: database.Now(),
})
if err != nil {
write(http.StatusInternalServerError, codersdk.Response{
Message: internalErrorMessage,
Detail: fmt.Sprintf("update user last_seen_at: %s", err.Error()),
})
return
}
}

// If the key is valid, we also fetch the user roles and status.
Expand Down
1 change: 1 addition & 0 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,7 @@ func convertUser(user database.User, organizationIDs []uuid.UUID) codersdk.User
ID: user.ID,
Email: user.Email,
CreatedAt: user.CreatedAt,
LastSeenAt: user.LastSeenAt,
Username: user.Username,
Status: codersdk.UserStatus(user.Status),
OrganizationIDs: organizationIDs,
Expand Down
29 changes: 29 additions & 0 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ func TestFirstUser(t *testing.T) {
_ = coderdtest.CreateFirstUser(t, client)
})

t.Run("LastSeenAt", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

client := coderdtest.New(t, nil)
firstUserResp := coderdtest.CreateFirstUser(t, client)

firstUser, err := client.User(ctx, firstUserResp.UserID.String())
require.NoError(t, err)

_ = coderdtest.CreateAnotherUser(t, client, firstUserResp.OrganizationID)

allUsers, err := client.Users(ctx, codersdk.UsersRequest{})
require.NoError(t, err)

require.Len(t, allUsers, 2)

// We sent the "GET Users" request with the first user, but the second user
// should be Never since they haven't performed a request.
for _, user := range allUsers {
if user.ID == firstUser.ID {
require.WithinDuration(t, firstUser.LastSeenAt, database.Now(), testutil.WaitShort)
} else {
require.Zero(t, user.LastSeenAt)
}
}
})

t.Run("AutoImportsTemplates", func(t *testing.T) {
t.Parallel()

Expand Down
10 changes: 6 additions & 4 deletions codersdk/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ type UsersRequest struct {

// User represents a user in Coder.
type User struct {
ID uuid.UUID `json:"id" validate:"required" table:"id"`
Username string `json:"username" validate:"required" table:"username"`
Email string `json:"email" validate:"required" table:"email"`
CreatedAt time.Time `json:"created_at" validate:"required" table:"created at"`
ID uuid.UUID `json:"id" validate:"required" table:"id"`
Username string `json:"username" validate:"required" table:"username"`
Email string `json:"email" validate:"required" table:"email"`
CreatedAt time.Time `json:"created_at" validate:"required" table:"created at"`
LastSeenAt time.Time `json:"last_seen_at"`

Status UserStatus `json:"status" table:"status"`
OrganizationIDs []uuid.UUID `json:"organization_ids"`
Roles []Role `json:"roles"`
Expand Down