Skip to content

Commit

Permalink
Merge pull request #8 from aiven/rkar-add-unit-test-coverage
Browse files Browse the repository at this point in the history
add unit test coverage
  • Loading branch information
roope-kar authored Oct 3, 2024
2 parents fa5a6e8 + f3c0125 commit a4ac844
Showing 1 changed file with 221 additions and 0 deletions.
221 changes: 221 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -194,3 +195,223 @@ func assertOutput(t *testing.T, name, actual, expected string) {
t.Errorf("Expected %s to match pattern: %q, but got: %q", name, expected, actual)
}
}

func getTestPlan(t *testing.T, path string) *Plan {
content, readErr := os.ReadFile(path)
if readErr != nil {
t.Fatal(readErr)
}

var plan Plan
if unmarshalErr := json.Unmarshal(content, &plan); unmarshalErr != nil {
t.Fatal("Invalid plan JSON file")
}

return &plan
}

func TestUnit_findExternalIdentity(t *testing.T) {
plan := getTestPlan(t, "testdata/plan_with_known_owner_user_group_id.json")

t.Run("Finds existing external_identity", func(t *testing.T) {
user := findExternalIdentity("alice", plan)
if user == nil {
t.Error()
}
})

t.Run("Returns nil if not found", func(t *testing.T) {
user := findExternalIdentity("frank", plan)
if user != nil {
t.Error()
}
})
}

func TestUnit_findApprovers(t *testing.T) {
plan := getTestPlan(t, "testdata/plan_with_known_owner_user_group_id.json")

t.Run("Finds existing external_identities for approvers", func(t *testing.T) {
approverIDs := make([]string, 0)
approverIDs = append(approverIDs, "bob")
resources := findApprovers(approverIDs, "alice", plan)
if len(resources) != 1 {
t.Error()
}
if resources[0].Values.ExternalUserID != "bob" {
t.Error()
}
})

t.Run("Filters out requester if present", func(t *testing.T) {
approverIDs := make([]string, 0)
approverIDs = append(approverIDs, "alice")
approverIDs = append(approverIDs, "bob")
resources := findApprovers(approverIDs, "alice", plan)
if len(resources) != 1 {
t.Error()
}
if resources[0].Values.ExternalUserID != "bob" {
t.Error()
}
})

t.Run("Does not return user if not found (nil)", func(t *testing.T) {
approverIDs := make([]string, 0)
approverIDs = append(approverIDs, "frank")
resources := findApprovers(approverIDs, "alice", plan)
if len(resources) != 0 {
t.Error()
}
})
}

func TestUnit_findOwnerAddressFromConfig(t *testing.T) {
plan := getTestPlan(t, "testdata/plan_with_known_owner_user_group_id.json")

t.Run("Return address if exists", func(t *testing.T) {
address := findOwnerAddressFromConfig("aiven_kafka_topic.foo", plan)
if *address != "aiven_organization_user_group.foo" {
t.Error()
}
})

t.Run("Return nil if not found", func(t *testing.T) {
address := findOwnerAddressFromConfig("aiven_kafka_topic.test", plan)
if address != nil {
t.Error()
}
})
}

func TestUnit_findUserAddressFromConfig(t *testing.T) {
plan := getTestPlan(t, "testdata/plan_with_known_owner_user_group_id.json")

t.Run("Return address if exists", func(t *testing.T) {
address := findUserAddressFromConfig("data.aiven_external_identity.alice", plan)
if address == nil {
t.Fatal()
}
if *address != "data.aiven_organization_user.foo" {
t.Error()
}
})

t.Run("Return nil if not found", func(t *testing.T) {
address := findUserAddressFromConfig("data.aiven_external_identity.frank", plan)
if address != nil {
t.Error()
}
})
}

func TestUnit_isUserGroupMemberFromState(t *testing.T) {
plan := getTestPlan(t, "testdata/plan_with_known_owner_user_group_id.json")

t.Run("Return true if exists", func(t *testing.T) {
topic := ChangeResource{}
ownerUserGroupID := "ug4e3b20cee48"
topic.OwnerUserGroupID = &ownerUserGroupID

user := StateResource{}
user.Values.InternalUserID = "u4e3706199a0"

ok := isUserGroupMemberFromState(topic, &user, plan)
if !ok {
t.Fatal()
}
})

t.Run("Return false if not found", func(t *testing.T) {
topic := ChangeResource{}
ownerUserGroupID := "ug4e3b20cee48"
topic.OwnerUserGroupID = &ownerUserGroupID

user := StateResource{}
user.Values.InternalUserID = "abc"

ok := isUserGroupMemberFromState(topic, &user, plan)
if ok {
t.Fatal()
}
})
}

func TestUnit_isUserGroupMemberFromConfig(t *testing.T) {
plan := getTestPlan(t, "testdata/plan_with_unknown_owner_user_group_id.json")

t.Run("Return true if exists", func(t *testing.T) {
topic := ResourceChange{}
topic.Address = "aiven_kafka_topic.foo"

user := StateResource{}
user.Address = "data.aiven_external_identity.alice"

ok := isUserGroupMemberFromConfig(topic, &user, plan)
if !ok {
t.Fatal()
}
})

t.Run("Return false if not found", func(t *testing.T) {
topic := ResourceChange{}
topic.Address = "aiven_kafka_topic.foo"

user := StateResource{}
user.Address = "data.aiven_external_identity.frank"

ok := isUserGroupMemberFromConfig(topic, &user, plan)
if ok {
t.Fatal()
}
})

t.Run("Return false if owner address not found", func(t *testing.T) {
topic := ResourceChange{}
topic.Address = "aiven_kafka_topic.magic"

user := StateResource{}
user.Address = "data.aiven_external_identity.frank"

ok := isUserGroupMemberFromConfig(topic, &user, plan)
if ok {
t.Fatal()
}
})

t.Run("Return false if user is nil", func(t *testing.T) {
topic := ResourceChange{}
topic.Address = "aiven_kafka_topic.foo"

ok := isUserGroupMemberFromConfig(topic, nil, plan)
if ok {
t.Fatal()
}
})
}

func TestUnit_newRequestError(t *testing.T) {
t.Run("Return error about requesting", func(t *testing.T) {
adress := "aiven_kafka_topic.foo"
err := newRequestError(adress)
if err.Error != "requesting user is not a member of the owner group" {
t.Error()
}
if err.Address != adress {
t.Error()
}
})
}

func TestUnit_newApproveError(t *testing.T) {
t.Run("Return error about approving", func(t *testing.T) {
adress := "aiven_kafka_topic.foo"
err := newApproveError(adress)
if err.Error != "approval is required from a member of the owner group" {
t.Error()
}
if err.Address != adress {
t.Error()
}
})
}

0 comments on commit a4ac844

Please sign in to comment.