From 7124c3a4c6c8681e86220644f9c2da7e114250b4 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Fri, 24 Aug 2012 12:32:23 +0100
Subject: [PATCH] When an email address is removed, make sure to remove
 loggedIn associations

---
 resources/static/common/js/storage.js            | 9 +++++++++
 resources/static/test/cases/common/js/storage.js | 9 ++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/resources/static/common/js/storage.js b/resources/static/common/js/storage.js
index e5fad5664..00ebea3ca 100644
--- a/resources/static/common/js/storage.js
+++ b/resources/static/common/js/storage.js
@@ -136,6 +136,15 @@ BrowserID.Storage = (function() {
         }
       }
       storage.siteInfo = JSON.stringify(siteInfo);
+
+      // remove any logged in sites associated with this address.
+      var loggedInInfo = JSON.parse(storage.loggedIn || "{}");
+      for(var site in loggedInInfo) {
+        if(loggedInInfo[site] === email) {
+          delete loggedInInfo[site];
+        }
+      }
+      storage.loggedIn = JSON.stringify(loggedInInfo);
     }
     else {
       throw "unknown email address";
diff --git a/resources/static/test/cases/common/js/storage.js b/resources/static/test/cases/common/js/storage.js
index cc98a58d9..56117820a 100644
--- a/resources/static/test/cases/common/js/storage.js
+++ b/resources/static/test/cases/common/js/storage.js
@@ -4,7 +4,9 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 (function() {
-  var storage = BrowserID.Storage,
+  var bid = BrowserID,
+      storage = bid.Storage,
+      testHelpers = bid.TestHelpers,
       TEST_ORIGIN = "http://test.domain";
 
   module("common/js/storage", {
@@ -183,11 +185,16 @@
 
   test("setLoggedIn, getLoggedIn, loggedInCount", function() {
     var email = "testuser@testuser.com";
+    storage.addSecondaryEmail(email);
     storage.setLoggedIn(TEST_ORIGIN, email);
     equal(storage.getLoggedIn(TEST_ORIGIN), email, "correct email");
 
     storage.setLoggedIn("http://another.domain", email);
     equal(storage.loggedInCount(), 2, "correct logged in count");
+
+    storage.removeEmail(email);
+    equal(storage.loggedInCount(), 0, "after email removed, not logged in anywhere");
+    testHelpers.testUndefined(storage.getLoggedIn(TEST_ORIGIN), "sites with email no longer logged in");
   });
 
 }());
-- 
GitLab