|
| 1 | +package mailcow |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +// TestMailboxQuotaNilHandling tests that nil quota values are handled correctly |
| 8 | +func TestMailboxQuotaNilHandling(t *testing.T) { |
| 9 | + testCases := []struct { |
| 10 | + name string |
| 11 | + quotaValue interface{} |
| 12 | + expectedQuota int |
| 13 | + shouldPanic bool |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "nil quota should return 0", |
| 17 | + quotaValue: nil, |
| 18 | + expectedQuota: 0, |
| 19 | + shouldPanic: false, |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "valid quota in bytes should convert to MB", |
| 23 | + quotaValue: float64(5368709120), // 5GB in bytes |
| 24 | + expectedQuota: 5120, // 5GB in MB |
| 25 | + shouldPanic: false, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "zero quota should return 0", |
| 29 | + quotaValue: float64(0), |
| 30 | + expectedQuota: 0, |
| 31 | + shouldPanic: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "small quota should round down", |
| 35 | + quotaValue: float64(1048575), // Just under 1MB |
| 36 | + expectedQuota: 0, |
| 37 | + shouldPanic: false, |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + for _, tc := range testCases { |
| 42 | + t.Run(tc.name, func(t *testing.T) { |
| 43 | + mailbox := map[string]interface{}{ |
| 44 | + "quota": tc.quotaValue, |
| 45 | + } |
| 46 | + |
| 47 | + // Simulate the quota conversion logic |
| 48 | + var quota int |
| 49 | + if mailbox["quota"] != nil { |
| 50 | + quota = int(mailbox["quota"].(float64)) / (1024 * 1024) |
| 51 | + } else { |
| 52 | + quota = 0 |
| 53 | + } |
| 54 | + |
| 55 | + if quota != tc.expectedQuota { |
| 56 | + t.Errorf("Expected quota %d, got %d", tc.expectedQuota, quota) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// TestMailboxAttributesNilHandling tests that nil attributes are handled correctly |
| 63 | +func TestMailboxAttributesNilHandling(t *testing.T) { |
| 64 | + testCases := []struct { |
| 65 | + name string |
| 66 | + attributesValue interface{} |
| 67 | + shouldProcess bool |
| 68 | + }{ |
| 69 | + { |
| 70 | + name: "nil attributes should be skipped", |
| 71 | + attributesValue: nil, |
| 72 | + shouldProcess: false, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "valid attributes should be processed", |
| 76 | + attributesValue: map[string]interface{}{ |
| 77 | + "force_pw_update": true, |
| 78 | + "sogo_access": true, |
| 79 | + }, |
| 80 | + shouldProcess: true, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "empty attributes map should be processed", |
| 84 | + attributesValue: map[string]interface{}{}, |
| 85 | + shouldProcess: true, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for _, tc := range testCases { |
| 90 | + t.Run(tc.name, func(t *testing.T) { |
| 91 | + mailbox := map[string]interface{}{ |
| 92 | + "attributes": tc.attributesValue, |
| 93 | + } |
| 94 | + |
| 95 | + // Simulate the attributes processing logic |
| 96 | + processed := false |
| 97 | + if mailbox["attributes"] != nil { |
| 98 | + _ = mailbox["attributes"].(map[string]interface{}) |
| 99 | + processed = true |
| 100 | + } |
| 101 | + |
| 102 | + if processed != tc.shouldProcess { |
| 103 | + t.Errorf("Expected processed=%v, got %v", tc.shouldProcess, processed) |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// TestMailboxDataIntegrity tests that mailbox data is correctly transformed |
| 110 | +func TestMailboxDataIntegrity(t *testing.T) { |
| 111 | + testCases := []struct { |
| 112 | + name string |
| 113 | + mailbox map[string]interface{} |
| 114 | + expected map[string]interface{} |
| 115 | + }{ |
| 116 | + { |
| 117 | + name: "complete mailbox data", |
| 118 | + mailbox: map[string]interface{}{ |
| 119 | + "name": "John Doe", |
| 120 | + "quota": float64(10737418240), // 10GB in bytes |
| 121 | + "attributes": map[string]interface{}{ |
| 122 | + "force_pw_update": true, |
| 123 | + "sogo_access": true, |
| 124 | + }, |
| 125 | + }, |
| 126 | + expected: map[string]interface{}{ |
| 127 | + "full_name": "John Doe", |
| 128 | + "quota": 10240, // 10GB in MB |
| 129 | + }, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "mailbox with nil quota and attributes", |
| 133 | + mailbox: map[string]interface{}{ |
| 134 | + "name": "Jane Doe", |
| 135 | + "quota": nil, |
| 136 | + "attributes": nil, |
| 137 | + }, |
| 138 | + expected: map[string]interface{}{ |
| 139 | + "full_name": "Jane Doe", |
| 140 | + "quota": 0, |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "mailbox with only name", |
| 145 | + mailbox: map[string]interface{}{ |
| 146 | + "name": "Test User", |
| 147 | + }, |
| 148 | + expected: map[string]interface{}{ |
| 149 | + "full_name": "Test User", |
| 150 | + "quota": 0, |
| 151 | + }, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + for _, tc := range testCases { |
| 156 | + t.Run(tc.name, func(t *testing.T) { |
| 157 | + // Simulate the transformation logic |
| 158 | + tc.mailbox["full_name"] = tc.mailbox["name"] |
| 159 | + if tc.mailbox["quota"] != nil { |
| 160 | + tc.mailbox["quota"] = int(tc.mailbox["quota"].(float64)) / (1024 * 1024) |
| 161 | + } else { |
| 162 | + tc.mailbox["quota"] = 0 |
| 163 | + } |
| 164 | + |
| 165 | + if tc.mailbox["full_name"] != tc.expected["full_name"] { |
| 166 | + t.Errorf("Expected full_name %v, got %v", tc.expected["full_name"], tc.mailbox["full_name"]) |
| 167 | + } |
| 168 | + |
| 169 | + if tc.mailbox["quota"] != tc.expected["quota"] { |
| 170 | + t.Errorf("Expected quota %v, got %v", tc.expected["quota"], tc.mailbox["quota"]) |
| 171 | + } |
| 172 | + }) |
| 173 | + } |
| 174 | +} |
0 commit comments