|
| 1 | +package autolink_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + |
| 9 | + "github.com/mattermost/mattermost-plugin-autolink/server/autolink" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + reVISA = `(?P<VISA>(?P<part1>4\d{3})[ -]?(?P<part2>\d{4})[ -]?(?P<part3>\d{4})[ -]?(?P<LastFour>[0-9]{4}))` |
| 14 | + reMasterCard = `(?P<MasterCard>(?P<part1>5[1-5]\d{2})[ -]?(?P<part2>\d{4})[ -]?(?P<part3>\d{4})[ -]?(?P<LastFour>[0-9]{4}))` |
| 15 | + reSwitchSolo = `(?P<SwitchSolo>(?P<part1>67\d{2})[ -]?(?P<part2>\d{4})[ -]?(?P<part3>\d{4})[ -]?(?P<LastFour>[0-9]{4}))` |
| 16 | + reDiscover = `(?P<Discover>(?P<part1>6011)[ -]?(?P<part2>\d{4})[ -]?(?P<part3>\d{4})[ -]?(?P<LastFour>[0-9]{4}))` |
| 17 | + reAMEX = `(?P<AMEX>(?P<part1>3[47]\d{2})[ -]?(?P<part2>\d{6})[ -]?(?P<part3>\d)(?P<LastFour>[0-9]{4}))` |
| 18 | + |
| 19 | + replaceVISA = "VISA XXXX-XXXX-XXXX-$LastFour" |
| 20 | + replaceMasterCard = "MasterCard XXXX-XXXX-XXXX-$LastFour" |
| 21 | + replaceSwitchSolo = "Switch/Solo XXXX-XXXX-XXXX-$LastFour" |
| 22 | + replaceDiscover = "Discover XXXX-XXXX-XXXX-$LastFour" |
| 23 | + replaceAMEX = "American Express XXXX-XXXXXX-X$LastFour" |
| 24 | +) |
| 25 | + |
| 26 | +func TestCreditCardRegex(t *testing.T) { |
| 27 | + for _, tc := range []struct { |
| 28 | + Name string |
| 29 | + RE string |
| 30 | + Replace string |
| 31 | + In string |
| 32 | + Out string |
| 33 | + }{ |
| 34 | + {"Visa happy spaces", reVISA, replaceVISA, " abc 4111 1111 1111 1234 def", " abc VISA XXXX-XXXX-XXXX-1234 def"}, |
| 35 | + {"Visa happy dashes", reVISA, replaceVISA, "4111-1111-1111-1234", "VISA XXXX-XXXX-XXXX-1234"}, |
| 36 | + {"Visa happy mixed", reVISA, replaceVISA, "41111111 1111-1234", "VISA XXXX-XXXX-XXXX-1234"}, |
| 37 | + {"Visa happy digits", reVISA, replaceVISA, "abc 4111111111111234 def", "abc VISA XXXX-XXXX-XXXX-1234 def"}, |
| 38 | + {"Visa non-match start", reVISA, replaceVISA, "3111111111111234", ""}, |
| 39 | + {"Visa non-match num digits", reVISA, replaceVISA, " 4111-1111-1111-123", ""}, |
| 40 | + {"Visa non-match sep", reVISA, replaceVISA, "4111=1111=1111_1234", ""}, |
| 41 | + {"Visa non-match no break before", reVISA, replaceVISA, "abc4111-1111-1111-1234", "abcVISA XXXX-XXXX-XXXX-1234"}, |
| 42 | + {"Visa non-match no break after", reVISA, replaceVISA, "4111-1111-1111-1234def", "VISA XXXX-XXXX-XXXX-1234def"}, |
| 43 | + |
| 44 | + {"MasterCard happy spaces", reMasterCard, replaceMasterCard, " abc 5111 1111 1111 1234 def", " abc MasterCard XXXX-XXXX-XXXX-1234 def"}, |
| 45 | + {"MasterCard happy dashes", reMasterCard, replaceMasterCard, "5211-1111-1111-1234", "MasterCard XXXX-XXXX-XXXX-1234"}, |
| 46 | + {"MasterCard happy mixed", reMasterCard, replaceMasterCard, "53111111 1111-1234", "MasterCard XXXX-XXXX-XXXX-1234"}, |
| 47 | + {"MasterCard happy digits", reMasterCard, replaceMasterCard, "abc 5411111111111234 def", "abc MasterCard XXXX-XXXX-XXXX-1234 def"}, |
| 48 | + {"MasterCard non-match start", reMasterCard, replaceMasterCard, "3111111111111234", ""}, |
| 49 | + {"MasterCard non-match num digits", reMasterCard, replaceMasterCard, " 5111-1111-1111-123", ""}, |
| 50 | + {"MasterCard non-match sep", reMasterCard, replaceMasterCard, "5111=1111=1111_1234", ""}, |
| 51 | + {"MasterCard non-match no break before", reMasterCard, replaceMasterCard, "abc5511-1111-1111-1234", "abcMasterCard XXXX-XXXX-XXXX-1234"}, |
| 52 | + {"MasterCard non-match no break after", reMasterCard, replaceMasterCard, "5111-1111-1111-1234def", "MasterCard XXXX-XXXX-XXXX-1234def"}, |
| 53 | + |
| 54 | + {"SwitchSolo happy spaces", reSwitchSolo, replaceSwitchSolo, " abc 6711 1111 1111 1234 def", " abc Switch/Solo XXXX-XXXX-XXXX-1234 def"}, |
| 55 | + {"SwitchSolo happy dashes", reSwitchSolo, replaceSwitchSolo, "6711-1111-1111-1234", "Switch/Solo XXXX-XXXX-XXXX-1234"}, |
| 56 | + {"SwitchSolo happy mixed", reSwitchSolo, replaceSwitchSolo, "67111111 1111-1234", "Switch/Solo XXXX-XXXX-XXXX-1234"}, |
| 57 | + {"SwitchSolo happy digits", reSwitchSolo, replaceSwitchSolo, "abc 6711111111111234 def", "abc Switch/Solo XXXX-XXXX-XXXX-1234 def"}, |
| 58 | + {"SwitchSolo non-match start", reSwitchSolo, replaceSwitchSolo, "3111111111111234", ""}, |
| 59 | + {"SwitchSolo non-match num digits", reSwitchSolo, replaceSwitchSolo, " 6711-1111-1111-123", ""}, |
| 60 | + {"SwitchSolo non-match sep", reSwitchSolo, replaceSwitchSolo, "6711=1111=1111_1234", ""}, |
| 61 | + {"SwitchSolo non-match no break before", reSwitchSolo, replaceSwitchSolo, "abc6711-1111-1111-1234", "abcSwitch/Solo XXXX-XXXX-XXXX-1234"}, |
| 62 | + {"SwitchSolo non-match no break after", reSwitchSolo, replaceSwitchSolo, "6711-1111-1111-1234def", "Switch/Solo XXXX-XXXX-XXXX-1234def"}, |
| 63 | + |
| 64 | + {"Discover happy spaces", reDiscover, replaceDiscover, " abc 6011 1111 1111 1234 def", " abc Discover XXXX-XXXX-XXXX-1234 def"}, |
| 65 | + {"Discover happy dashes", reDiscover, replaceDiscover, "6011-1111-1111-1234", "Discover XXXX-XXXX-XXXX-1234"}, |
| 66 | + {"Discover happy mixed", reDiscover, replaceDiscover, "60111111 1111-1234", "Discover XXXX-XXXX-XXXX-1234"}, |
| 67 | + {"Discover happy digits", reDiscover, replaceDiscover, "abc 6011111111111234 def", "abc Discover XXXX-XXXX-XXXX-1234 def"}, |
| 68 | + {"Discover non-match start", reDiscover, replaceDiscover, "3111111111111234", ""}, |
| 69 | + {"Discover non-match num digits", reDiscover, replaceDiscover, " 6011-1111-1111-123", ""}, |
| 70 | + {"Discover non-match sep", reDiscover, replaceDiscover, "6011=1111=1111_1234", ""}, |
| 71 | + {"Discover non-match no break before", reDiscover, replaceDiscover, "abc6011-1111-1111-1234", "abcDiscover XXXX-XXXX-XXXX-1234"}, |
| 72 | + {"Discover non-match no break after", reDiscover, replaceDiscover, "6011-1111-1111-1234def", "Discover XXXX-XXXX-XXXX-1234def"}, |
| 73 | + |
| 74 | + {"AMEX happy spaces", reAMEX, replaceAMEX, " abc 3411 123456 12345 def", " abc American Express XXXX-XXXXXX-X2345 def"}, |
| 75 | + {"AMEX happy dashes", reAMEX, replaceAMEX, "3711-123456-12345", "American Express XXXX-XXXXXX-X2345"}, |
| 76 | + {"AMEX happy mixed", reAMEX, replaceAMEX, "3411-123456 12345", "American Express XXXX-XXXXXX-X2345"}, |
| 77 | + {"AMEX happy digits", reAMEX, replaceAMEX, "abc 371112345612345 def", "abc American Express XXXX-XXXXXX-X2345 def"}, |
| 78 | + {"AMEX non-match start 41", reAMEX, replaceAMEX, "411112345612345", ""}, |
| 79 | + {"AMEX non-match start 31", reAMEX, replaceAMEX, "3111111111111234", ""}, |
| 80 | + {"AMEX non-match num digits", reAMEX, replaceAMEX, " 4111-1111-1111-123", ""}, |
| 81 | + {"AMEX non-match sep", reAMEX, replaceAMEX, "4111-1111=1111-1234", ""}, |
| 82 | + {"AMEX non-match no break before", reAMEX, replaceAMEX, "abc3711-123456-12345", "abcAmerican Express XXXX-XXXXXX-X2345"}, |
| 83 | + {"AMEX non-match no break after", reAMEX, replaceAMEX, "3711-123456-12345def", "American Express XXXX-XXXXXX-X2345def"}, |
| 84 | + } { |
| 85 | + t.Run(tc.Name, func(t *testing.T) { |
| 86 | + re := regexp.MustCompile(tc.RE) |
| 87 | + result := re.ReplaceAllString(tc.In, tc.Replace) |
| 88 | + if tc.Out != "" { |
| 89 | + assert.Equal(t, tc.Out, result) |
| 90 | + } else { |
| 91 | + assert.Equal(t, tc.In, result) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestCreditCard(t *testing.T) { |
| 98 | + var tests = []struct { |
| 99 | + Name string |
| 100 | + Link autolink.Autolink |
| 101 | + inputMessage string |
| 102 | + expectedMessage string |
| 103 | + }{ |
| 104 | + { |
| 105 | + "VISA happy", |
| 106 | + autolink.Autolink{ |
| 107 | + Pattern: reVISA, |
| 108 | + Template: replaceVISA, |
| 109 | + }, |
| 110 | + "A credit card 4111-1111-2222-1234 mentioned", |
| 111 | + "A credit card VISA XXXX-XXXX-XXXX-1234 mentioned", |
| 112 | + }, { |
| 113 | + "VISA", |
| 114 | + autolink.Autolink{ |
| 115 | + Pattern: reVISA, |
| 116 | + Template: replaceVISA, |
| 117 | + DisableNonWordPrefix: true, |
| 118 | + DisableNonWordSuffix: true, |
| 119 | + }, |
| 120 | + "A credit card4111-1111-2222-3333mentioned", |
| 121 | + "A credit cardVISA XXXX-XXXX-XXXX-3333mentioned", |
| 122 | + }, { |
| 123 | + "Multiple VISA replacements", |
| 124 | + autolink.Autolink{ |
| 125 | + Pattern: reVISA, |
| 126 | + Template: replaceVISA, |
| 127 | + }, |
| 128 | + "Credit cards 4111-1111-2222-3333 4222-3333-4444-5678 mentioned", |
| 129 | + "Credit cards VISA XXXX-XXXX-XXXX-3333 VISA XXXX-XXXX-XXXX-5678 mentioned", |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + for _, tt := range tests { |
| 134 | + t.Run(tt.Name, func(t *testing.T) { |
| 135 | + err := tt.Link.Compile() |
| 136 | + actual := tt.Link.Replace(tt.inputMessage) |
| 137 | + |
| 138 | + assert.Equal(t, tt.expectedMessage, actual) |
| 139 | + assert.NoError(t, err) |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
0 commit comments