drone/internal/api/handler/user/find_test.go
Johannes Batzill 8c2f900c80 Principals, ServiceAccounts, Tokens and auth.Sessions (#15)
This change introduces the concept of a principal (abstraction of call identity), and adds a new service account type principal. Also adds support for different tokens (session, PAT, SAT, OAuth2) and adds auth.Session which is being used to capture information about the caller and call method.
2022-09-25 23:44:51 -07:00

56 lines
1.4 KiB
Go

// Copyright 2021 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package user
import (
"encoding/json"
"net/http/httptest"
"testing"
"github.com/golang/mock/gomock"
"github.com/harness/gitness/internal/api/request"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/mocks"
"github.com/harness/gitness/types"
"github.com/google/go-cmp/cmp"
)
func TestFind(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
mockUser := &types.User{
ID: 1,
Email: "octocat@github.com",
}
users := mocks.NewMockUserStore(controller)
users.EXPECT().Find(gomock.Any(), mockUser.ID).Return(mockUser, nil)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/v1/user", nil)
r = r.WithContext(
request.WithAuthSession(
request.WithUser(
r.Context(),
mockUser),
&auth.Session{Principal: *types.PrincipalFromUser(mockUser), Metadata: &auth.EmptyMetadata{}}),
)
HandleFind(w, r)
if got, want := w.Code, 200; want != got {
t.Errorf("Want response code %d, got %d", want, got)
}
got, want := &types.User{}, mockUser
if err := json.NewDecoder(w.Body).Decode(got); err != nil {
t.Error(err)
}
if diff := cmp.Diff(got, want); len(diff) != 0 {
t.Errorf(diff)
}
}