import pytest
import json

from data import model
from endpoints.api import api
from endpoints.api.test.shared import conduct_api_call
from endpoints.api.robot import UserRobot, OrgRobot
from endpoints.test.shared import client_with_identity

from test.test_ldap import mock_ldap

from test.fixtures import *

@pytest.mark.parametrize('endpoint', [
  UserRobot,
  OrgRobot,
])
@pytest.mark.parametrize('body', [
  {},
  {'description': 'this is a description'},
  {'unstructured_metadata': {'foo': 'bar'}},
  {'description': 'this is a description', 'unstructured_metadata': {'foo': 'bar'}},
])
def test_create_robot_with_metadata(endpoint, body, client):
  with client_with_identity('devtable', client) as cl:
    # Create the robot with the specified body.
    conduct_api_call(cl, endpoint, 'PUT', {'orgname': 'buynlarge', 'robot_shortname': 'somebot'},
                     body, expected_code=201)

    # Ensure the create succeeded.
    resp = conduct_api_call(cl, endpoint, 'GET', {
      'orgname': 'buynlarge',
      'robot_shortname': 'somebot',
    })

    body = body or {}
    assert resp.json['description'] == (body.get('description') or '')
    assert resp.json['unstructured_metadata'] == (body.get('unstructured_metadata') or {})