|
| 1 | +package attestation_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/l3montree-dev/devguard/internal/core" |
| 10 | + "github.com/l3montree-dev/devguard/internal/core/attestation" |
| 11 | + "github.com/l3montree-dev/devguard/internal/database/models" |
| 12 | + "github.com/l3montree-dev/devguard/mocks" |
| 13 | + "github.com/labstack/echo/v4" |
| 14 | + "github.com/stretchr/testify/mock" |
| 15 | +) |
| 16 | + |
| 17 | +func TestList(t *testing.T) { |
| 18 | + t.Run("everything works as expected", func(t *testing.T) { |
| 19 | + req := httptest.NewRequest("GET", "/webhook", bytes.NewBufferString("")) |
| 20 | + e := echo.New() |
| 21 | + ctx := e.NewContext(req, httptest.NewRecorder()) |
| 22 | + |
| 23 | + asset := models.Asset{} |
| 24 | + core.SetAsset(ctx, asset) |
| 25 | + |
| 26 | + attestationRepository := mocks.NewCoreAttestationRepository(t) |
| 27 | + attestationRepository.On("GetByAssetID", mock.Anything).Return([]models.Attestation{ |
| 28 | + {AttestationName: "not ocol name"}, |
| 29 | + }, nil) |
| 30 | + attestationController := attestation.NewAttestationController(attestationRepository) |
| 31 | + result := attestationController.List(ctx) |
| 32 | + if result != nil { |
| 33 | + t.Fail() |
| 34 | + } |
| 35 | + |
| 36 | + }) |
| 37 | + t.Run("getByAssetID returns an error so we should also receive an error", func(t *testing.T) { |
| 38 | + req := httptest.NewRequest("GET", "/webhook", bytes.NewBufferString("")) |
| 39 | + e := echo.New() |
| 40 | + ctx := e.NewContext(req, httptest.NewRecorder()) |
| 41 | + |
| 42 | + asset := models.Asset{} |
| 43 | + core.SetAsset(ctx, asset) |
| 44 | + |
| 45 | + attestationRepository := mocks.NewCoreAttestationRepository(t) |
| 46 | + attestationRepository.On("GetByAssetID", mock.Anything).Return([]models.Attestation{}, fmt.Errorf("Something went wrong")) |
| 47 | + attestationController := attestation.NewAttestationController(attestationRepository) |
| 48 | + |
| 49 | + result := attestationController.List(ctx) |
| 50 | + |
| 51 | + if result == nil { |
| 52 | + t.Fail() |
| 53 | + } |
| 54 | + |
| 55 | + }) |
| 56 | +} |
0 commit comments