|
| 1 | +package endtoend_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "ctRestClient/internal/testutil" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | +) |
| 13 | + |
| 14 | +var blocklistsDir string |
| 15 | + |
| 16 | +var _ = Describe("running ctRestClient with configured blocklist", func() { |
| 17 | + |
| 18 | + BeforeEach(func() { |
| 19 | + configContent := testutil.YamlToString(fmt.Sprintf(` |
| 20 | + --- |
| 21 | + instances: |
| 22 | + - hostname: %s |
| 23 | + token_name: %s |
| 24 | + groups: |
| 25 | + - name: testGroup1 |
| 26 | + fields: [firstName, lastName, street, zip, city, sexId, birthday] |
| 27 | + `, ctInstanceUrl, ctInstanceUrl)) |
| 28 | + |
| 29 | + configPath = filepath.Join(tempDir, "geburtstage-config.yml") |
| 30 | + err = os.WriteFile(configPath, []byte(configContent), 0644) |
| 31 | + Expect(err).ToNot(HaveOccurred()) |
| 32 | + |
| 33 | + blocklistsDir = filepath.Join(dataDir, "blocklists") |
| 34 | + err := os.MkdirAll(blocklistsDir, 0755) |
| 35 | + Expect(err).NotTo(HaveOccurred()) |
| 36 | + }) |
| 37 | + |
| 38 | + var _ = Describe("with an empty blocklist", func() { |
| 39 | + BeforeEach(func() { |
| 40 | + emptyBlocklist := testutil.YamlToString(``) |
| 41 | + err = os.WriteFile(filepath.Join(blocklistsDir, "testGroup1.yml"), []byte(emptyBlocklist), 0644) |
| 42 | + Expect(err).NotTo(HaveOccurred()) |
| 43 | + }) |
| 44 | + |
| 45 | + It("should create the expected csv files", func() { |
| 46 | + runBinary() |
| 47 | + |
| 48 | + csvString := csvFileToString("testGroup1.csv") |
| 49 | + |
| 50 | + Expect(csvString).To(Equal(`firstName;lastName;street;zip;city;sexId;birthday |
| 51 | +Anna;Mustermann;Musterstraße 1;11111;Musterstadt;2;2008-09-12 |
| 52 | +Max;Beispiel;Beispielweg 2;22222;Musterdorf;1;2008-09-24 |
| 53 | +Lisa;Tester;Testallee 3;33333;Mustertal;2;2008-09-29 |
| 54 | +Tom;Probe;Probestraße 4/1;44444;Musterhausen;1;2008-09-18 |
| 55 | +`)) |
| 56 | + }) |
| 57 | + |
| 58 | + It("should log the expected output", func() { |
| 59 | + expectedLog := fmt.Sprintf(`%s |
| 60 | +[INFO] +----------------------------------------------------------------------+ |
| 61 | +[INFO] | Processing instance '%s' | |
| 62 | +[INFO] +----------------------------------------------------------------------+ |
| 63 | +[INFO] |
| 64 | +[INFO] processing group 'testGroup1' |
| 65 | +[INFO] the group has 4 persons |
| 66 | +[INFO] using blocklist 'testGroup1.yml' |
| 67 | +[INFO] blocked 0 persons |
| 68 | +`, LOG_HEADER, ctInstanceUrl) |
| 69 | + |
| 70 | + runBinary() |
| 71 | + |
| 72 | + normalizedLines := getNormalizedLogLines(logFilePath()) |
| 73 | + expectedLogLines := strings.Split(expectedLog, "\n") |
| 74 | + |
| 75 | + for i, line := range normalizedLines { |
| 76 | + Expect(line).To(Equal(expectedLogLines[i]), fmt.Sprintf("Mismatch at line %d:\nExpected: %s\nActual: %s", i+1, expectedLogLines[i], line)) |
| 77 | + } |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + var _ = Describe("with a not matching blocklist", func() { |
| 82 | + BeforeEach(func() { |
| 83 | + emptyBlocklist := testutil.YamlToString(` |
| 84 | + - street: "Non Matching Street" |
| 85 | + `) |
| 86 | + err = os.WriteFile(filepath.Join(blocklistsDir, "testGroup1.yml"), []byte(emptyBlocklist), 0644) |
| 87 | + Expect(err).NotTo(HaveOccurred()) |
| 88 | + }) |
| 89 | + |
| 90 | + It("should create the expected csv files", func() { |
| 91 | + runBinary() |
| 92 | + |
| 93 | + csvString := csvFileToString("testGroup1.csv") |
| 94 | + |
| 95 | + Expect(csvString).To(Equal(`firstName;lastName;street;zip;city;sexId;birthday |
| 96 | +Anna;Mustermann;Musterstraße 1;11111;Musterstadt;2;2008-09-12 |
| 97 | +Max;Beispiel;Beispielweg 2;22222;Musterdorf;1;2008-09-24 |
| 98 | +Lisa;Tester;Testallee 3;33333;Mustertal;2;2008-09-29 |
| 99 | +Tom;Probe;Probestraße 4/1;44444;Musterhausen;1;2008-09-18 |
| 100 | +`)) |
| 101 | + }) |
| 102 | + |
| 103 | + It("should log the expected output", func() { |
| 104 | + expectedLog := fmt.Sprintf(`%s |
| 105 | +[INFO] +----------------------------------------------------------------------+ |
| 106 | +[INFO] | Processing instance '%s' | |
| 107 | +[INFO] +----------------------------------------------------------------------+ |
| 108 | +[INFO] |
| 109 | +[INFO] processing group 'testGroup1' |
| 110 | +[INFO] the group has 4 persons |
| 111 | +[INFO] using blocklist 'testGroup1.yml' |
| 112 | +[INFO] blocked 0 persons |
| 113 | +`, LOG_HEADER, ctInstanceUrl) |
| 114 | + |
| 115 | + runBinary() |
| 116 | + |
| 117 | + normalizedLines := getNormalizedLogLines(logFilePath()) |
| 118 | + expectedLogLines := strings.Split(expectedLog, "\n") |
| 119 | + |
| 120 | + for i, line := range normalizedLines { |
| 121 | + Expect(line).To(Equal(expectedLogLines[i]), fmt.Sprintf("Mismatch at line %d:\nExpected: %s\nActual: %s", i+1, expectedLogLines[i], line)) |
| 122 | + } |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + var _ = Describe("with matching blocklist", func() { |
| 127 | + BeforeEach(func() { |
| 128 | + emptyBlocklist := testutil.YamlToString(` |
| 129 | + - street: "Testallee 3" |
| 130 | + `) |
| 131 | + err = os.WriteFile(filepath.Join(blocklistsDir, "testGroup1.yml"), []byte(emptyBlocklist), 0644) |
| 132 | + Expect(err).NotTo(HaveOccurred()) |
| 133 | + }) |
| 134 | + |
| 135 | + It("should create the expected csv files", func() { |
| 136 | + runBinary() |
| 137 | + |
| 138 | + csvString := csvFileToString("testGroup1.csv") |
| 139 | + |
| 140 | + Expect(csvString).To(Equal(`firstName;lastName;street;zip;city;sexId;birthday |
| 141 | +Anna;Mustermann;Musterstraße 1;11111;Musterstadt;2;2008-09-12 |
| 142 | +Max;Beispiel;Beispielweg 2;22222;Musterdorf;1;2008-09-24 |
| 143 | +Tom;Probe;Probestraße 4/1;44444;Musterhausen;1;2008-09-18 |
| 144 | +`)) |
| 145 | + }) |
| 146 | + |
| 147 | + It("should log the expected output", func() { |
| 148 | + expectedLog := fmt.Sprintf(`%s |
| 149 | +[INFO] +----------------------------------------------------------------------+ |
| 150 | +[INFO] | Processing instance '%s' | |
| 151 | +[INFO] +----------------------------------------------------------------------+ |
| 152 | +[INFO] |
| 153 | +[INFO] processing group 'testGroup1' |
| 154 | +[INFO] the group has 4 persons |
| 155 | +[INFO] using blocklist 'testGroup1.yml' |
| 156 | +[INFO] -> "Lisa" "Tester" will not be added to csv file |
| 157 | +[INFO] blocked 1 persons |
| 158 | +`, LOG_HEADER, ctInstanceUrl) |
| 159 | + |
| 160 | + runBinary() |
| 161 | + |
| 162 | + normalizedLines := getNormalizedLogLines(logFilePath()) |
| 163 | + expectedLogLines := strings.Split(expectedLog, "\n") |
| 164 | + |
| 165 | + for i, line := range normalizedLines { |
| 166 | + Expect(line).To(Equal(expectedLogLines[i]), fmt.Sprintf("Mismatch at line %d:\nExpected: %s\nActual: %s", i+1, expectedLogLines[i], line)) |
| 167 | + } |
| 168 | + }) |
| 169 | + }) |
| 170 | +}) |
0 commit comments