Skip to content

Commit

Permalink
test: integration test for audit trail API
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasslash committed Jun 1, 2022
1 parent f1aa97e commit 926ab25
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
96 changes: 96 additions & 0 deletions audit_trail_integration_test.go
@@ -0,0 +1,96 @@
//go:build integration
// +build integration

package tfe

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAuditTrailsList(t *testing.T) {
skipIfEnterprise(t)

client := testClient(t)
ctx := context.Background()

org, orgCleanup := createOrganization(t, client)
t.Cleanup(orgCleanup)

upgradeOrganizationSubscription(t, client, org)

time.Sleep(10 * time.Second)

orgToken, orgTokenCleanup := createOrganizationToken(t, client, org)
t.Cleanup(orgTokenCleanup)

// First let's generate some audit events in this test organization
_, wkspace1Cleanup := createWorkspace(t, client, org)
t.Cleanup(wkspace1Cleanup)

_, wkspace2Cleanup := createWorkspace(t, client, org)
t.Cleanup(wkspace2Cleanup)

t.Run("with no specified timeframe", func(t *testing.T) {
atl, err := client.AuditTrails.List(ctx, orgToken.Token, nil)
require.NoError(t, err)
require.Greater(t, len(atl.Items), 0)

log := atl.Items[0]
assert.NotEmpty(t, log.ID)
assert.NotEmpty(t, log.Timestamp)
assert.NotEmpty(t, log.Type)
assert.NotEmpty(t, log.Version)
assert.NotNil(t, log.Resource)
assert.NotNil(t, log.Auth)
assert.NotNil(t, log.Request)

t.Run("with resource deserialized correctly", func(t *testing.T) {
assert.NotEmpty(t, log.Resource.ID)
assert.NotEmpty(t, log.Resource.Type)
assert.NotEmpty(t, log.Resource.Action)

// we don't test against log.Resource.Meta since we don't know the nature
// of the audit trail log we're testing against as it can be nil or contain a k-v map
})

t.Run("with auth deserialized correctly", func(t *testing.T) {
assert.NotEmpty(t, log.Auth.AccessorID)
assert.NotEmpty(t, log.Auth.Description)
assert.NotEmpty(t, log.Auth.Type)
assert.NotEmpty(t, log.Auth.OrganizationID)
})

t.Run("with request deserialized correctly", func(t *testing.T) {
assert.NotEmpty(t, log.Request.ID)
})
})

t.Run("using since query param", func(t *testing.T) {
since := time.Now()

// Let's create an event that is sent to the audit log
_, wsCleanup := createWorkspace(t, client, org)
t.Cleanup(wsCleanup)

atl, err := client.AuditTrails.List(ctx, orgToken.Token, &AuditTrailListOptions{
Since: since,
ListOptions: &ListOptions{
PageNumber: 1,
PageSize: 20,
},
})
require.NoError(t, err)

assert.LessOrEqual(t, len(atl.Items), 20)
assert.Greater(t, len(atl.Items), 0)

for _, log := range atl.Items {
assert.True(t, log.Timestamp.After(since))
}
})
}
29 changes: 29 additions & 0 deletions helper_test.go
Expand Up @@ -1274,6 +1274,35 @@ func waitForSVOutputs(t *testing.T, client *Client, svID string) {
wg.Wait()
}

func waitForAuditEvents(t *testing.T, client *Client, orgToken string) {
t.Helper()
wg := &sync.WaitGroup{}
wg.Add(1)

go func() {
_, err := retry(func() (interface{}, error) {
atl, err := client.AuditTrails.List(context.Background(), orgToken, nil)
if err != nil {
return nil, err
}

if len(atl.Items) == 0 {
return nil, errors.New("no audit events found")
}

return atl, nil
})
if err != nil {
t.Error(err)
}

wg.Done()
}()

wg.Wait()

}

func waitForRunLock(t *testing.T, client *Client, workspaceID string) {
t.Helper()
wg := &sync.WaitGroup{}
Expand Down

0 comments on commit 926ab25

Please sign in to comment.