@@ -21,6 +21,8 @@ module Agents |
||
| 21 | 21 |
* `this.memory()` |
| 22 | 22 |
* `this.memory(key)` |
| 23 | 23 |
* `this.memory(keyToSet, valueToSet)` |
| 24 |
+ * `this.credential(name)` |
|
| 25 |
+ * `this.credential(name, valueToSet)` |
|
| 24 | 26 |
* `this.options()` |
| 25 | 27 |
* `this.options(key)` |
| 26 | 28 |
* `this.log(message)` |
@@ -120,6 +122,8 @@ module Agents |
||
| 120 | 122 |
end |
| 121 | 123 |
context["escapeHtml"] = lambda { |a, x| CGI.escapeHTML(x) }
|
| 122 | 124 |
context["unescapeHtml"] = lambda { |a, x| CGI.unescapeHTML(x) }
|
| 125 |
+ context['getCredential'] = lambda { |a, k| credential(k); }
|
|
| 126 |
+ context['setCredential'] = lambda { |a, k, v| set_credential(k, v) }
|
|
| 123 | 127 |
|
| 124 | 128 |
if (options['language'] || '').downcase == 'coffeescript' |
| 125 | 129 |
context.eval(CoffeeScript.compile code) |
@@ -142,6 +146,12 @@ module Agents |
||
| 142 | 146 |
(interpolated['code'] || '').strip =~ /\Acredential:(.*)\Z/ && $1 |
| 143 | 147 |
end |
| 144 | 148 |
|
| 149 |
+ def set_credential(name, value) |
|
| 150 |
+ c = user.user_credentials.find_or_initialize_by(credential_name: name) |
|
| 151 |
+ c.credential_value = value |
|
| 152 |
+ c.save! |
|
| 153 |
+ end |
|
| 154 |
+ |
|
| 145 | 155 |
def setup_javascript |
| 146 | 156 |
<<-JS |
| 147 | 157 |
function Agent() {};
|
@@ -164,6 +174,14 @@ module Agents |
||
| 164 | 174 |
} |
| 165 | 175 |
} |
| 166 | 176 |
|
| 177 |
+ Agent.credential = function(name, value) {
|
|
| 178 |
+ if (typeof(value) !== "undefined") {
|
|
| 179 |
+ setCredential(name, value); |
|
| 180 |
+ } else {
|
|
| 181 |
+ return getCredential(name); |
|
| 182 |
+ } |
|
| 183 |
+ } |
|
| 184 |
+ |
|
| 167 | 185 |
Agent.options = function(key) {
|
| 168 | 186 |
if (typeof(key) !== "undefined") {
|
| 169 | 187 |
return JSON.parse(getOptions())[key]; |
@@ -264,5 +264,33 @@ describe Agents::JavaScriptAgent do |
||
| 264 | 264 |
expect(AgentLog.last.message).to eq("hello from coffeescript")
|
| 265 | 265 |
end |
| 266 | 266 |
end |
| 267 |
+ |
|
| 268 |
+ describe "user credentials" do |
|
| 269 |
+ it "can access an existing credential" do |
|
| 270 |
+ @agent.send(:set_credential, 'test', 'hello') |
|
| 271 |
+ @agent.options['code'] = 'Agent.check = function() { this.log(this.credential("test")); };'
|
|
| 272 |
+ @agent.save! |
|
| 273 |
+ @agent.check |
|
| 274 |
+ expect(AgentLog.last.message).to eq("hello")
|
|
| 275 |
+ end |
|
| 276 |
+ |
|
| 277 |
+ it "will create a new credential" do |
|
| 278 |
+ @agent.options['code'] = 'Agent.check = function() { this.credential("test","1234"); };'
|
|
| 279 |
+ @agent.save! |
|
| 280 |
+ expect {
|
|
| 281 |
+ @agent.check |
|
| 282 |
+ }.to change(UserCredential, :count).by(1) |
|
| 283 |
+ end |
|
| 284 |
+ |
|
| 285 |
+ it "updates an existing credential" do |
|
| 286 |
+ @agent.send(:set_credential, 'test', 1234) |
|
| 287 |
+ @agent.options['code'] = 'Agent.check = function() { this.credential("test","12345"); };'
|
|
| 288 |
+ @agent.save! |
|
| 289 |
+ expect {
|
|
| 290 |
+ @agent.check |
|
| 291 |
+ }.to change(UserCredential, :count).by(0) |
|
| 292 |
+ expect(@agent.user.user_credentials.last.credential_value).to eq('12345')
|
|
| 293 |
+ end |
|
| 294 |
+ end |
|
| 267 | 295 |
end |
| 268 | 296 |
end |