|
| 1 | +import Quick |
| 2 | +import Nimble |
| 3 | + |
| 4 | +@testable import Auth0 |
| 5 | + |
| 6 | +class SensitiveDataRedactorSpec: QuickSpec { |
| 7 | + |
| 8 | + override class func spec() { |
| 9 | + |
| 10 | + describe("SensitiveDataRedactor") { |
| 11 | + |
| 12 | + context("redact()") { |
| 13 | + |
| 14 | + it("should redact access_token") { |
| 15 | + let json = """ |
| 16 | + { |
| 17 | + "access_token": "secret_access_token_value", |
| 18 | + "token_type": "Bearer", |
| 19 | + "expires_in": 86400 |
| 20 | + } |
| 21 | + """ |
| 22 | + let data = json.data(using: .utf8)! |
| 23 | + |
| 24 | + let result = SensitiveDataRedactor.redact(data) |
| 25 | + |
| 26 | + expect(result).toNot(beNil()) |
| 27 | + expect(result).to(contain("<REDACTED>")) |
| 28 | + expect(result).toNot(contain("secret_access_token_value")) |
| 29 | + expect(result).to(contain("token_type")) |
| 30 | + expect(result).to(contain("expires_in")) |
| 31 | + } |
| 32 | + |
| 33 | + it("should redact id_token") { |
| 34 | + let json = """ |
| 35 | + { |
| 36 | + "id_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature", |
| 37 | + "token_type": "Bearer" |
| 38 | + } |
| 39 | + """ |
| 40 | + let data = json.data(using: .utf8)! |
| 41 | + |
| 42 | + let result = SensitiveDataRedactor.redact(data) |
| 43 | + |
| 44 | + expect(result).toNot(beNil()) |
| 45 | + expect(result).to(contain("<REDACTED>")) |
| 46 | + expect(result).toNot(contain("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9")) |
| 47 | + } |
| 48 | + |
| 49 | + it("should redact refresh_token") { |
| 50 | + let json = """ |
| 51 | + { |
| 52 | + "access_token": "access_value", |
| 53 | + "refresh_token": "refresh_value" |
| 54 | + } |
| 55 | + """ |
| 56 | + let data = json.data(using: .utf8)! |
| 57 | + |
| 58 | + let result = SensitiveDataRedactor.redact(data) |
| 59 | + |
| 60 | + expect(result).toNot(beNil()) |
| 61 | + expect(result).to(contain("<REDACTED>")) |
| 62 | + expect(result).toNot(contain("access_value")) |
| 63 | + expect(result).toNot(contain("refresh_value")) |
| 64 | + } |
| 65 | + |
| 66 | + it("should redact multiple sensitive fields") { |
| 67 | + let json = """ |
| 68 | + { |
| 69 | + "access_token": "at_123", |
| 70 | + "id_token": "it_456", |
| 71 | + "refresh_token": "rt_789", |
| 72 | + "scope": "openid profile email" |
| 73 | + } |
| 74 | + """ |
| 75 | + let data = json.data(using: .utf8)! |
| 76 | + |
| 77 | + let result = SensitiveDataRedactor.redact(data) |
| 78 | + |
| 79 | + expect(result).toNot(beNil()) |
| 80 | + expect(result).to(contain("<REDACTED>")) |
| 81 | + expect(result).toNot(contain("at_123")) |
| 82 | + expect(result).toNot(contain("it_456")) |
| 83 | + expect(result).toNot(contain("rt_789")) |
| 84 | + expect(result).to(contain("scope")) |
| 85 | + expect(result).to(contain("openid profile email")) |
| 86 | + } |
| 87 | + |
| 88 | + it("should preserve non-sensitive fields") { |
| 89 | + let json = """ |
| 90 | + { |
| 91 | + "user_id": "auth0|123456", |
| 92 | + |
| 93 | + "access_token": "secret" |
| 94 | + } |
| 95 | + """ |
| 96 | + let data = json.data(using: .utf8)! |
| 97 | + |
| 98 | + let result = SensitiveDataRedactor.redact(data) |
| 99 | + |
| 100 | + expect(result).toNot(beNil()) |
| 101 | + expect(result).to(contain("user_id")) |
| 102 | + expect(result).to(contain("auth0|123456")) |
| 103 | + expect(result).to(contain("email")) |
| 104 | + expect(result ).to(contain("[email protected]")) |
| 105 | + expect(result).toNot(contain("secret")) |
| 106 | + } |
| 107 | + |
| 108 | + it("should return non-JSON String for non-JSON data") { |
| 109 | + let plainText = "This is not JSON" |
| 110 | + let data = plainText.data(using: .utf8)! |
| 111 | + |
| 112 | + let result = SensitiveDataRedactor.redact(data) |
| 113 | + |
| 114 | + expect(result).to(contain(plainText)) |
| 115 | + } |
| 116 | + |
| 117 | + it("should handle empty JSON") { |
| 118 | + let json = "{}" |
| 119 | + let data = json.data(using: .utf8)! |
| 120 | + |
| 121 | + let result = SensitiveDataRedactor.redact(data) |
| 122 | + |
| 123 | + expect(result).toNot(beNil()) |
| 124 | + let expectedJson = try! JSONSerialization.data(withJSONObject: [:], options: [.prettyPrinted]) |
| 125 | + let expected = String(data: expectedJson, encoding: .utf8)! |
| 126 | + expect(result).to(equal(expected)) |
| 127 | + } |
| 128 | + |
| 129 | + it("should handle JSON with no sensitive fields") { |
| 130 | + let json = """ |
| 131 | + { |
| 132 | + "username": "john", |
| 133 | + "age": 30 |
| 134 | + } |
| 135 | + """ |
| 136 | + let data = json.data(using: .utf8)! |
| 137 | + |
| 138 | + let result = SensitiveDataRedactor.redact(data) |
| 139 | + |
| 140 | + expect(result).toNot(beNil()) |
| 141 | + expect(result).to(contain("username")) |
| 142 | + expect(result).to(contain("john")) |
| 143 | + expect(result).to(contain("age")) |
| 144 | + expect(result).toNot(contain("<REDACTED>")) |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments