|
| 1 | +// |
| 2 | +// Copyright 2024 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package biz_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/chainloop-dev/chainloop/app/controlplane/internal/biz" |
| 23 | + "github.com/chainloop-dev/chainloop/app/controlplane/internal/biz/testhelpers" |
| 24 | + "github.com/google/uuid" |
| 25 | + "github.com/stretchr/testify/suite" |
| 26 | +) |
| 27 | + |
| 28 | +func (s *robotAccountTestSuite) TestRevoke() { |
| 29 | + ctx := context.Background() |
| 30 | + s.Run("returns an error if org ID format is not valid", func() { |
| 31 | + err := s.RobotAccount.Revoke(ctx, "not_valid_uuid", uuid.NewString()) |
| 32 | + s.ErrorAs(err, &biz.ErrInvalidUUID{}) |
| 33 | + }) |
| 34 | + |
| 35 | + s.Run("returns an error if robot account ID format is not valid", func() { |
| 36 | + err := s.RobotAccount.Revoke(ctx, uuid.NewString(), "not_valid_uuid") |
| 37 | + s.ErrorAs(err, &biz.ErrInvalidUUID{}) |
| 38 | + }) |
| 39 | + |
| 40 | + s.Run("returns a Not Found if robot account cannot be found", func() { |
| 41 | + err := s.RobotAccount.Revoke(ctx, s.org.ID, uuid.NewString()) |
| 42 | + s.ErrorAs(err, &biz.ErrNotFound{}) |
| 43 | + }) |
| 44 | + |
| 45 | + s.Run("revokes the robot account", func() { |
| 46 | + err := s.RobotAccount.Revoke(ctx, s.org.ID, s.ra.ID.String()) |
| 47 | + s.NoError(err) |
| 48 | + |
| 49 | + // Reload the robot account |
| 50 | + ra, err := s.RobotAccount.FindByID(ctx, s.ra.ID.String()) |
| 51 | + s.NoError(err) |
| 52 | + s.NotNil(ra.RevokedAt) |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +// Utility struct to hold the test suite |
| 57 | +type robotAccountTestSuite struct { |
| 58 | + testhelpers.UseCasesEachTestSuite |
| 59 | + org *biz.Organization |
| 60 | + ra *biz.RobotAccount |
| 61 | +} |
| 62 | + |
| 63 | +// // Run the tests |
| 64 | +func TestRobotAccountUseCase(t *testing.T) { |
| 65 | + suite.Run(t, new(robotAccountTestSuite)) |
| 66 | +} |
| 67 | + |
| 68 | +func (s *robotAccountTestSuite) SetupTest() { |
| 69 | + var err error |
| 70 | + ctx := context.Background() |
| 71 | + |
| 72 | + s.TestingUseCases = testhelpers.NewTestingUseCases(s.T()) |
| 73 | + s.org, err = s.Organization.CreateWithRandomName(ctx) |
| 74 | + s.NoError(err) |
| 75 | + |
| 76 | + wf, err := s.Workflow.Create(ctx, &biz.WorkflowCreateOpts{ |
| 77 | + Name: "myworkflow", |
| 78 | + OrgID: s.org.ID, |
| 79 | + }) |
| 80 | + s.NoError(err) |
| 81 | + s.ra, err = s.RobotAccount.Create(ctx, "myRobotAccount", s.org.ID, wf.ID.String()) |
| 82 | + s.NoError(err) |
| 83 | +} |
0 commit comments