Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: integration test for audit trail API
- Loading branch information
1 parent
f1aa97e
commit 926ab25
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters