| 1 |
*** Settings *** |
| 2 |
Library OperatingSystem |
| 3 |
Resource lib.robot |
| 4 |
|
| 5 |
*** Variables *** |
| 6 |
${CONFIG} ${RSPAMD_TESTDIR}/configs/multiclass_bayes.conf |
| 7 |
${MESSAGE_HAM} ${RSPAMD_TESTDIR}/messages/ham.eml |
| 8 |
${MESSAGE_SPAM} ${RSPAMD_TESTDIR}/messages/spam_message.eml |
| 9 |
${MESSAGE_NEWSLETTER} ${RSPAMD_TESTDIR}/messages/newsletter.eml |
| 10 |
${REDIS_SCOPE} Suite |
| 11 |
${RSPAMD_REDIS_SERVER} null |
| 12 |
${RSPAMD_SCOPE} Suite |
| 13 |
${RSPAMD_STATS_BACKEND} redis |
| 14 |
${RSPAMD_STATS_HASH} null |
| 15 |
${RSPAMD_STATS_KEY} null |
| 16 |
${RSPAMD_STATS_PER_USER} ${EMPTY} |
| 17 |
|
| 18 |
*** Keywords *** |
| 19 |
Learn Multiclass |
| 20 |
[Arguments] ${user} ${class} ${message} |
| 21 |
# Extract filename from message path for queue-id |
| 22 |
${path} ${filename} = Split Path ${message} |
| 23 |
IF "${user}" |
| 24 |
${result} = Run Rspamc -d ${user} -h ${RSPAMD_LOCAL_ADDR}:${RSPAMD_PORT_CONTROLLER} learn_class:${class} ${message} |
| 25 |
ELSE |
| 26 |
${result} = Run Rspamc -h ${RSPAMD_LOCAL_ADDR}:${RSPAMD_PORT_CONTROLLER} learn_class:${class} ${message} |
| 27 |
END |
| 28 |
Check Rspamc ${result} |
| 29 |
|
| 30 |
Learn Multiclass Legacy |
| 31 |
[Arguments] ${user} ${class} ${message} |
| 32 |
# Test backward compatibility with old learn_spam/learn_ham commands |
| 33 |
# Extract filename from message path for queue-id |
| 34 |
${path} ${filename} = Split Path ${message} |
| 35 |
IF "${user}" |
| 36 |
${result} = Run Rspamc -d ${user} -h ${RSPAMD_LOCAL_ADDR}:${RSPAMD_PORT_CONTROLLER} learn_${class} ${message} |
| 37 |
ELSE |
| 38 |
${result} = Run Rspamc -h ${RSPAMD_LOCAL_ADDR}:${RSPAMD_PORT_CONTROLLER} learn_${class} ${message} |
| 39 |
END |
| 40 |
Check Rspamc ${result} |
| 41 |
|
| 42 |
Multiclass Basic Learn Test |
| 43 |
[Arguments] ${user}=${EMPTY} |
| 44 |
Set Suite Variable ${RSPAMD_STATS_LEARNTEST} 0 |
| 45 |
Set Test Variable ${kwargs} &{EMPTY} |
| 46 |
IF "${user}" |
| 47 |
Set To Dictionary ${kwargs} Deliver-To=${user} |
| 48 |
END |
| 49 |
|
| 50 |
# Learn all classes |
| 51 |
Learn Multiclass ${user} spam ${MESSAGE_SPAM} |
| 52 |
Learn Multiclass ${user} ham ${MESSAGE_HAM} |
| 53 |
Learn Multiclass ${user} newsletter ${MESSAGE_NEWSLETTER} |
| 54 |
|
| 55 |
# Test classification |
| 56 |
Scan File ${MESSAGE_SPAM} &{kwargs} |
| 57 |
Expect Symbol BAYES_SPAM |
| 58 |
|
| 59 |
Scan File ${MESSAGE_HAM} &{kwargs} |
| 60 |
Expect Symbol BAYES_HAM |
| 61 |
|
| 62 |
Scan File ${MESSAGE_NEWSLETTER} &{kwargs} |
| 63 |
Expect Symbol BAYES_NEWSLETTER |
| 64 |
|
| 65 |
Set Suite Variable ${RSPAMD_STATS_LEARNTEST} 1 |
| 66 |
|
| 67 |
Multiclass Legacy Compatibility Test |
| 68 |
[Arguments] ${user}=${EMPTY} |
| 69 |
Set Test Variable ${kwargs} &{EMPTY} |
| 70 |
IF "${user}" |
| 71 |
Set To Dictionary ${kwargs} Deliver-To=${user} |
| 72 |
END |
| 73 |
|
| 74 |
# Test legacy learn_spam and learn_ham commands still work |
| 75 |
Learn Multiclass Legacy ${user} spam ${MESSAGE_SPAM} |
| 76 |
Learn Multiclass Legacy ${user} ham ${MESSAGE_HAM} |
| 77 |
|
| 78 |
# Should still classify correctly |
| 79 |
Scan File ${MESSAGE_SPAM} &{kwargs} |
| 80 |
Expect Symbol BAYES_SPAM |
| 81 |
|
| 82 |
Scan File ${MESSAGE_HAM} &{kwargs} |
| 83 |
Expect Symbol BAYES_HAM |
| 84 |
|
| 85 |
Multiclass Relearn Test |
| 86 |
[Arguments] ${user}=${EMPTY} |
| 87 |
IF ${RSPAMD_STATS_LEARNTEST} == 0 |
| 88 |
Fail "Learn test was not run" |
| 89 |
END |
| 90 |
|
| 91 |
Set Test Variable ${kwargs} &{EMPTY} |
| 92 |
IF "${user}" |
| 93 |
Set To Dictionary ${kwargs} Deliver-To=${user} |
| 94 |
END |
| 95 |
|
| 96 |
# Relearn spam message as ham |
| 97 |
Learn Multiclass ${user} ham ${MESSAGE_SPAM} |
| 98 |
|
| 99 |
# Should now classify as ham or at least not spam |
| 100 |
Scan File ${MESSAGE_SPAM} &{kwargs} |
| 101 |
${pass} = Run Keyword And Return Status Expect Symbol BAYES_HAM |
| 102 |
IF ${pass} |
| 103 |
Pass Execution Successfully reclassified spam as ham |
| 104 |
END |
| 105 |
Do Not Expect Symbol BAYES_SPAM |
| 106 |
|
| 107 |
Multiclass Cross-Learn Test |
| 108 |
[Arguments] ${user}=${EMPTY} |
| 109 |
Set Test Variable ${kwargs} &{EMPTY} |
| 110 |
IF "${user}" |
| 111 |
Set To Dictionary ${kwargs} Deliver-To=${user} |
| 112 |
END |
| 113 |
|
| 114 |
# Learn newsletter message as ham to test cross-class learning |
| 115 |
Learn Multiclass ${user} ham ${MESSAGE_NEWSLETTER} |
| 116 |
|
| 117 |
# Should classify as ham, not newsletter (since we trained it as ham) |
| 118 |
Scan File ${MESSAGE_NEWSLETTER} &{kwargs} |
| 119 |
Expect Symbol BAYES_HAM |
| 120 |
Do Not Expect Symbol BAYES_NEWSLETTER |
| 121 |
|
| 122 |
Multiclass Unlearn Test |
| 123 |
[Arguments] ${user}=${EMPTY} |
| 124 |
Set Test Variable ${kwargs} &{EMPTY} |
| 125 |
IF "${user}" |
| 126 |
Set To Dictionary ${kwargs} Deliver-To=${user} |
| 127 |
END |
| 128 |
|
| 129 |
# First learn spam |
| 130 |
Learn Multiclass ${user} spam ${MESSAGE_SPAM} |
| 131 |
Scan File ${MESSAGE_SPAM} &{kwargs} |
| 132 |
Expect Symbol BAYES_SPAM |
| 133 |
|
| 134 |
# Then unlearn spam (learn as ham) |
| 135 |
Learn Multiclass ${user} ham ${MESSAGE_SPAM} |
| 136 |
|
| 137 |
# Should no longer classify as spam |
| 138 |
Scan File ${MESSAGE_SPAM} &{kwargs} |
| 139 |
Do Not Expect Symbol BAYES_SPAM |
| 140 |
|
| 141 |
Check Multiclass Results |
| 142 |
[Arguments] ${result} ${expected_class} |
| 143 |
# Check that scan result contains expected class information |
| 144 |
Should Contain ${result.stdout} BAYES_${expected_class.upper()} |
| 145 |
# Check for multiclass result format [class_name] |
| 146 |
Should Match Regexp ${result.stdout} BAYES_${expected_class.upper()}.*\\[${expected_class}\\] |
| 147 |
|
| 148 |
Multiclass Stats Test |
| 149 |
# Check that rspamc stat shows learning counts for all classes |
| 150 |
${result} = Run Rspamc -h ${RSPAMD_LOCAL_ADDR}:${RSPAMD_PORT_CONTROLLER} stat |
| 151 |
# Don't use Check Rspamc for stat command as it expects JSON success format |
| 152 |
Should Be Equal As Integers ${result.rc} 0 |
| 153 |
|
| 154 |
# Should show statistics for all classes |
| 155 |
Should Contain ${result.stdout} BAYES_SPAM |
| 156 |
Should Contain ${result.stdout} BAYES_HAM |
| 157 |
Should Contain ${result.stdout} BAYES_NEWSLETTER |
| 158 |
|
| 159 |
Multiclass Configuration Migration Test |
| 160 |
# Test that old binary config can be automatically migrated |
| 161 |
Set Test Variable ${binary_config} ${RSPAMD_TESTDIR}/configs/stats.conf |
| 162 |
|
| 163 |
# Start with binary config |
| 164 |
${result} = Run Rspamc --config ${binary_config} stat |
| 165 |
Check Rspamc ${result} |
| 166 |
|
| 167 |
# Should show deprecation warning but work |
| 168 |
Should Contain ${result.stderr} deprecated ignore_case=True |
| 169 |
|