(t *testing.T)
| 580 | } |
| 581 | |
| 582 | func TestGetCurrentUser_ServiceAccount(t *testing.T) { |
| 583 | a := require.New(t) |
| 584 | ctx := context.Background() |
| 585 | ctl := &controller{} |
| 586 | ctx, err := ctl.StartServerWithExternalPg(ctx) |
| 587 | a.NoError(err) |
| 588 | defer ctl.Close(ctx) |
| 589 | |
| 590 | // Create a dummy user to get the workspace reference. |
| 591 | userResp, err := ctl.userServiceClient.CreateUser(ctx, connect.NewRequest(&v1pb.CreateUserRequest{ |
| 592 | User: &v1pb.User{ |
| 593 | Title: "dummy", |
| 594 | Email: "dummy@bytebase.com", |
| 595 | Password: "1024bytebase", |
| 596 | }, |
| 597 | })) |
| 598 | a.NoError(err) |
| 599 | workspace := userResp.Msg.Workspace |
| 600 | |
| 601 | // Get actuator info using the workspace reference. |
| 602 | actuator, err := ctl.actuatorServiceClient.GetActuatorInfo(ctx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{ |
| 603 | Name: workspace, |
| 604 | })) |
| 605 | a.NoError(err) |
| 606 | |
| 607 | // Create a service account. |
| 608 | saResp, err := ctl.serviceAccountServiceClient.CreateServiceAccount(ctx, connect.NewRequest(&v1pb.CreateServiceAccountRequest{ |
| 609 | Parent: actuator.Msg.Workspace, |
| 610 | ServiceAccountId: "sa-test", |
| 611 | ServiceAccount: &v1pb.ServiceAccount{ |
| 612 | Title: "SA Test", |
| 613 | }, |
| 614 | })) |
| 615 | a.NoError(err) |
| 616 | sa := saResp.Msg |
| 617 | |
| 618 | // Grant the service account workspace admin role. |
| 619 | _, err = ctl.addMemberToWorkspaceIAM(ctx, actuator.Msg.Workspace, fmt.Sprintf("serviceAccount:%v", sa.Email), "roles/workspaceAdmin") |
| 620 | a.NoError(err) |
| 621 | |
| 622 | // Login as the service account using its service_key as password. |
| 623 | loginResp, err := ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{ |
| 624 | Email: sa.Email, |
| 625 | Password: sa.ServiceKey, |
| 626 | })) |
| 627 | a.NoError(err) |
| 628 | |
| 629 | // Swap the controller token to the service account's token. |
| 630 | originalToken := ctl.authInterceptor.token |
| 631 | ctl.authInterceptor.token = loginResp.Msg.Token |
| 632 | defer func() { |
| 633 | ctl.authInterceptor.token = originalToken |
| 634 | }() |
| 635 | |
| 636 | meResp, err := ctl.userServiceClient.GetCurrentUser(ctx, connect.NewRequest(&emptypb.Empty{})) |
| 637 | |
| 638 | // Assertions per the task specification. |
| 639 | a.NoError(err) |
nothing calls this directly
no test coverage detected