Skip to content

Commit

Permalink
refactor: replace harcoded values with generateTerraformResource func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
giovannibaratta committed Dec 8, 2023
1 parent a6ba251 commit 8398439
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {FilePlanReader} from "@libs/external/file-plan-reader/file-plan-reader"
import {Test} from "@nestjs/testing"
import "expect-more-jest"
import {Logger} from "@nestjs/common"
import {generateTerraformResource} from "@libs/testing/random"

jest.mock("fs", () => fs)

Expand All @@ -22,9 +23,8 @@ describe("FilePlanReader", () => {
it("should read the plan and return the diffs", async () => {
// Given
const planLocation = "/plan.json"
const resourceType = "aws_instance"
const resourceName = "example"
const resourceAddress = "unique_resource_address"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()
const planContent = {
resource_changes: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Action, TerraformDiff} from "@libs/domain/terraform/diffs"
import {TerraformEntity} from "@libs/domain/terraform/resource"
import {RequireApprovalModeUseCase} from "@libs/service/approval/require-approval-mode.use-case"
import {mockConfiguration} from "@libs/testing/mocks/configuration.mock"
import {generateTerraformResource} from "@libs/testing/random"
import {Test, TestingModule} from "@nestjs/testing"

describe("RequireApprovalModeUseCase", () => {
Expand All @@ -22,9 +23,8 @@ describe("RequireApprovalModeUseCase", () => {
describe("isApprovalRequired", () => {
it("should return true if the plan contains a plain resource that requires approval", async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down Expand Up @@ -96,9 +96,8 @@ describe("RequireApprovalModeUseCase", () => {

it("should return true if the plan contains a plain resource that requires approval and the action matches", async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down Expand Up @@ -137,9 +136,8 @@ describe("RequireApprovalModeUseCase", () => {

it("should return false if the plan contains a plain resource but the action does not match", async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down Expand Up @@ -178,9 +176,8 @@ describe("RequireApprovalModeUseCase", () => {

it("should return true if the plan contains a diff type included in the global match actions", async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down Expand Up @@ -222,9 +219,8 @@ describe("RequireApprovalModeUseCase", () => {

it("should return true if the provider type is included in the global match provider types", async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Action, TerraformDiff} from "@libs/domain/terraform/diffs"
import {TerraformEntity} from "@libs/domain/terraform/resource"
import {SafeToApplyModeUseCase} from "@libs/service/approval/safe-to-apply-mode.use-case"
import {mockConfiguration} from "@libs/testing/mocks/configuration.mock"
import {generateTerraformResource} from "@libs/testing/random"
import {Test, TestingModule} from "@nestjs/testing"

describe("SafeToApplyModeUseCase", () => {
Expand All @@ -22,9 +23,8 @@ describe("SafeToApplyModeUseCase", () => {
describe("isApprovalRequired", () => {
it("should return true if the plan contains resource that are not safe to apply", async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down Expand Up @@ -65,9 +65,8 @@ describe("SafeToApplyModeUseCase", () => {
"resources that are safe to apply (defined in the decorator)",
async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down Expand Up @@ -109,9 +108,8 @@ describe("SafeToApplyModeUseCase", () => {
"resources that are safe to apply (defined in the global rules)",
async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down Expand Up @@ -157,9 +155,8 @@ describe("SafeToApplyModeUseCase", () => {
"resources that are safe to apply (defined in the global rules and the decorator)",
async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down Expand Up @@ -203,9 +200,8 @@ describe("SafeToApplyModeUseCase", () => {

it("should return false if the plan contains only actions specified in the decorator", async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down Expand Up @@ -244,9 +240,8 @@ describe("SafeToApplyModeUseCase", () => {

it("should return true if the plan contains actions that are not specified in the decorator", async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down Expand Up @@ -286,9 +281,8 @@ describe("SafeToApplyModeUseCase", () => {

it("should return false if the plan contains resources of type that are safe to apply", async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"
const {resourceType, resourceName, resourceAddress} =
generateTerraformResource()

const terraformEntity: TerraformEntity = {
entityInfo: {
Expand Down

0 comments on commit 8398439

Please sign in to comment.