From c73f11de1cfaea6490cc7913f632c5ea3fc5a2e1 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Fri, 14 Sep 2012 13:12:41 +0100
Subject: [PATCH] Add rp_api stat to the dialog.

---
 .../common/js/models/interaction_data.js      |  3 +-
 resources/static/dialog/js/modules/dialog.js  | 21 ++++++++
 .../common/js/models/interaction_data.js      |  6 ++-
 .../test/cases/dialog/js/modules/dialog.js    | 50 +++++++++++++++++++
 4 files changed, 77 insertions(+), 3 deletions(-)

diff --git a/resources/static/common/js/models/interaction_data.js b/resources/static/common/js/models/interaction_data.js
index c1df7522c..d40da5139 100644
--- a/resources/static/common/js/models/interaction_data.js
+++ b/resources/static/common/js/models/interaction_data.js
@@ -22,7 +22,8 @@ BrowserID.Models.InteractionData = (function() {
         'number_sites_remembered',
         'orphaned',
         'new_account',
-        'email_type'
+        'email_type',
+        'rp_api'
       ];
 
 
diff --git a/resources/static/dialog/js/modules/dialog.js b/resources/static/dialog/js/modules/dialog.js
index 6566944dd..00d6e8f8b 100644
--- a/resources/static/dialog/js/modules/dialog.js
+++ b/resources/static/dialog/js/modules/dialog.js
@@ -110,6 +110,20 @@ BrowserID.Modules.Dialog = (function() {
     throw "must be an absolute path: (" + path + ")";
   }
 
+  function validateRPAPI(rpAPI) {
+    var VALID_RP_API_VALUES = [
+      "watch_without_onready",
+      "watch_with_onready",
+      "get",
+      "getVerifiedEmail",
+      "internal"
+    ];
+
+    if (_.indexOf(VALID_RP_API_VALUES, rpAPI) === -1) {
+      throw "invalid value for rp_api: " + rpAPI;
+    }
+  }
+
   var Dialog = bid.Modules.PageModule.extend({
     start: function(options) {
       var self=this;
@@ -170,6 +184,13 @@ BrowserID.Modules.Dialog = (function() {
 
       // verify params
       try {
+        var rpAPI = paramsFromRP.rp_api;
+        if (rpAPI) {
+          // throws if an invalid rp_api value
+          validateRPAPI(rpAPI);
+          self.publish("kpi_data", { rp_api: rpAPI });
+        }
+
         if (paramsFromRP.requiredEmail) {
           helpers.log("requiredEmail has been deprecated");
         }
diff --git a/resources/static/test/cases/common/js/models/interaction_data.js b/resources/static/test/cases/common/js/models/interaction_data.js
index deab52638..c0d79f4f4 100644
--- a/resources/static/test/cases/common/js/models/interaction_data.js
+++ b/resources/static/test/cases/common/js/models/interaction_data.js
@@ -100,7 +100,8 @@
           number_sites_remembered: 3,
           orphaned: false,
           new_account: true,
-          email_type: "assertion"
+          email_type: "assertion",
+          rp_api: "watch_without_onready"
         });
         model.stageCurrent();
 
@@ -123,7 +124,8 @@
             number_sites_remembered: 3,
             orphaned: false,
             new_account: true,
-            email_type: "assertion"
+            email_type: "assertion",
+            rp_api: "watch_without_onready"
           });
 
           testHelpers.testUndefined(mostRecentSessionData.local_timestamp, "non-whitelisted valued stripped");
diff --git a/resources/static/test/cases/dialog/js/modules/dialog.js b/resources/static/test/cases/dialog/js/modules/dialog.js
index 5bb1d19f4..052f05da0 100644
--- a/resources/static/test/cases/dialog/js/modules/dialog.js
+++ b/resources/static/test/cases/dialog/js/modules/dialog.js
@@ -68,6 +68,35 @@
     controller.start(options);
   }
 
+  function testMessageNotExpected(msg) {
+    mediator.subscribe(msg, function(msg, info) {
+      ok(false, "unexpected message: " + msg);
+    });
+  }
+
+  function testExpectGetFailure(options, expectedErrorMessage) {
+    _.extend(options, {
+      ready: function() {
+        testMessageNotExpected("kpi_data");
+        testMessageNotExpected("start");
+
+        var retval = controller.get(HTTPS_TEST_DOMAIN, options);
+
+        if (expectedErrorMessage) {
+          equal(retval, expectedErrorMessage, "expected error: " + expectedErrorMessage);
+        }
+        else {
+          ok(retval, "error message returned");
+        }
+
+        testErrorVisible();
+        start();
+      }
+    });
+    createController(options);
+  }
+
+
   module("dialog/js/modules/dialog", {
     setup: function() {
       winMock = new WinMock();
@@ -620,5 +649,26 @@
       }
     });
   });
+
+  asyncTest("get with valid rp_api - allowed", function() {
+    createController({
+      ready: function() {
+        mediator.subscribe("kpi_data", function(msg, info) {
+          equal(info.rp_api, "get");
+          start();
+        });
+
+        controller.get(HTTPS_TEST_DOMAIN, {
+          rp_api: "get"
+        });
+      }
+    });
+  });
+
+  asyncTest("get with invalid rp_api - not allowed", function() {
+    testExpectGetFailure({
+      rp_api: "invalid_value"
+    }, "invalid value for rp_api: invalid_value");
+  });
 }());
 
-- 
GitLab