summaryrefslogtreecommitdiffstats
path: root/toolkit/identity/tests/unit/test_observer_topics.js
blob: 8e5a89c91ac9caf29131ac02d1c2bf660f952f68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * By their nature, these tests duplicate some of the functionality of
 * other tests for Identity, RelyingParty, and IdentityProvider.
 *
 * In particular, "identity-auth-complete" and
 * "identity-login-state-changed" are tested in test_authentication.js
 */

"use strict";

XPCOMUtils.defineLazyModuleGetter(this, "IDService",
                                  "resource://gre/modules/identity/Identity.jsm",
                                  "IdentityService");

function test_smoke() {
  do_check_neq(IDService, null);
  run_next_test();
}

function test_identity_request() {
  // In response to navigator.id.request(), initiate a login with user
  // interaction by notifying observers of 'identity-request'

  do_test_pending();

  IDService.reset();

  let id = "landru@mockmyid.com";
  setup_test_identity(id, TEST_CERT, function() {
    // deliberately adding a trailing final slash on the domain
    // to test path composition
    let mockedDoc = mock_doc(null, "http://jed.gov/", function() {});

    // by calling watch() we create an rp flow.
    IDService.RP.watch(mockedDoc);

    // register the request UX observer
    makeObserver("identity-request", function (aSubject, aTopic, aData) {
      do_check_eq(aTopic, "identity-request");
      do_check_eq(aData, null);

      // check that all the URLs are properly resolved
      let subj = aSubject.wrappedJSObject;
      do_check_eq(subj.privacyPolicy, "http://jed.gov/pp.html");
      do_check_eq(subj.termsOfService, "http://jed.gov/tos.html");

      do_test_finished();
      run_next_test();
    });

    let requestOptions = {
      privacyPolicy: "/pp.html",
      termsOfService: "/tos.html"
    };
    IDService.RP.request(mockedDoc.id, requestOptions);
  });

}

function test_identity_auth() {
  // see test_authentication.js for "identity-auth-complete"
  // and "identity-login-state-changed"

  do_test_pending();
  let _provId = "bogus";

  // Simulate what would be returned by IDService._fetchWellKnownFile
  // for a given domain.
  let idpParams = {
    domain: "myfavoriteflan.com",
    idpParams: {
      authentication: "/foo/authenticate.html",
      provisioning: "/foo/provision.html"
    }
  };

  // Create an RP flow
  let mockedDoc = mock_doc(null, TEST_URL, function(action, params) {});
  IDService.RP.watch(mockedDoc);

  // The identity-auth notification is sent up to the UX from the
  // _doAuthentication function.  Be ready to receive it and call
  // beginAuthentication
  makeObserver("identity-auth", function (aSubject, aTopic, aData) {
    do_check_neq(aSubject, null);
    do_check_eq(aTopic, "identity-auth");
    do_check_eq(aData, "https://myfavoriteflan.com/foo/authenticate.html");

    do_check_eq(aSubject.wrappedJSObject.provId, _provId);
    do_test_finished();
    run_next_test();
  });

  // Even though our provisioning flow id is bogus, IdentityProvider
  // won't look at it until farther along in the authentication
  // process.  So this test can pass with a fake provId.
  IDService.IDP._doAuthentication(_provId, idpParams);
}

var TESTS = [
    test_smoke,
    test_identity_request,
    test_identity_auth,
  ];


TESTS.forEach(add_test);

function run_test() {
  run_next_test();
}