<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<Module>
  <ModulePrefs title="EndToEndTest">
    <Require feature="osapi" />
  </ModulePrefs>
  <Content type="html">
    <![CDATA[
      <script type="text/javascript" src="/testframework.js"></script>
      <script type="text/javascript">
        var tests = {
          /** Test fetching a specific ID */
          fetchId: function() {
            function receivedData(response) {
              var data = gadgets.json.stringify(response);
              assertFalse("Should not have error - " + data, response.error);
              assertEquals("Names don't match - " + data, "Shin Digg", response.displayName);
              finished();
            }
            osapi.people.get({ userId : 'canonical', fields : ["id", "displayName"]}).execute(receivedData);
          },

          /** Test fetching the owner, which should be "canonical" */
          fetchOwner: function() {
            function receivedData(response) {
              var data = gadgets.json.stringify(response);
              assertFalse("Should not have error - " + data, response.error);
              assertEquals("Names don't match - " + data, "Shin Digg", response.displayName);
              finished();
            }
            osapi.people.getOwner({ userId : 'canonical', fields : ["id", "displayName"]}).execute(receivedData);
          },

          /** Test fetching the viewer, which should be "john.doe" */
          fetchViewer: function() {
            function receivedData(response) {
              var data = gadgets.json.stringify(response);
              assertFalse("Should not have error - " + data, response.error);
              assertEquals("Names don't match - " + data, "Johnny", response.displayName);
              finished();
            }
            osapi.people.getViewer({ userId : 'canonical', fields : ["id", "displayName"]}).execute(receivedData);
          },

          /** Test fetching the owner and viewer as a batch */
          fetchOwnerAndViewer: function() {
            function receivedData(response) {
              var data = gadgets.json.stringify(response);
              assertFalse("Should not have error - " + data, response.error);
              assertEquals("Names don't match - " + data, "Shin Digg", response.list[0].displayName);
              assertEquals("Names don't match - " + data, "Johnny", response.list[1].displayName);
              finished();
            }
            osapi.people.get({ userId : ['@owner', '@viewer'], fields : ["id", "displayName"]}).execute(receivedData);
          },

          fetchPersonNotFound: function() {
            function receivedData(response) {
              var data = gadgets.json.stringify(response);
              assertTrue("Should have error - " + data, response.error);
              assertEquals("Not a badRequest - " + data, "400", response.error.code);
              finished();
            }

            osapi.people.get({ userId : "not.a.real.id", fields : ["id", "displayName"]}).execute(receivedData);
          },

          /** Test updating a person */
          updatePersonWhenViewerAllowed: function() {
            function receivedData(response) {
              var data = gadgets.json.stringify(response);
              assertFalse('Data error', response.error);
              assertEquals("Ids don't match - " + data, "john.doe", response.id);
              assertEquals("Thumbnails URLs don't match - " + data, "http://url.com", response.thumbnailUrl);
              finished();
            }

            // viewer is "john.doe" from security token
            osapi.people.update({person : {thumbnailUrl: "http://url.com"}}).execute(receivedData);
          },

          /** Test updating a person */
          updatePersonWhenViewerNotAllowed: function() {
            function receivedData(response) {
              var data = gadgets.json.stringify(response);
              assertTrue('Should have error - ' + data, response.error);
              assertEquals("Forbidden " + data, "403", response.error.code);
              finished();
            }

            // viewer is "john.doe" from security token
            osapi.people.update({userId: "canonical", person : {thumbnailUrl: "http://url.com"}}).execute(receivedData);
          },

        };

      </script>
    ]]>
  </Content>
</Module>
