#!/usr/bin/env vpython
# Copyright 2014 The LUCI Authors. All rights reserved.
# Use of this source code is governed under the Apache License, Version 2.0
# that can be found in the LICENSE file.

import base64
import datetime
import sys
import unittest

from test_support import test_env
test_env.setup_test_env()

import mock

from google.appengine.ext import ndb

from components import auth
from components import net
from components.config import remote
from test_support import test_case

import test_config_pb2


class RemoteTestCase(test_case.TestCase):
  def setUp(self):
    super(RemoteTestCase, self).setUp()
    self.mock(net, 'json_request_async', mock.Mock())
    net.json_request_async.side_effect = self.json_request_async

    self.provider = remote.Provider('luci-config.appspot.com')
    provider_future = ndb.Future()
    provider_future.set_result(self.provider)
    self.mock(remote, 'get_provider_async', lambda: provider_future)

  @ndb.tasklet
  def json_request_async(self, url, **kwargs):
    assert kwargs['scopes']
    URL_PREFIX = 'https://luci-config.appspot.com/_ah/api/config/v1/'
    if url == URL_PREFIX + 'config_sets/services%2Ffoo/config/bar.cfg':
      assert kwargs['params']['hash_only']
      raise ndb.Return({
        'content_hash': 'deadbeef',
        'revision': 'aaaabbbb',
      })
    if url == URL_PREFIX + 'config_sets/services%2Ffoo/config/baz.cfg':
      assert kwargs['params']['hash_only']
      raise ndb.Return({
        'content_hash': 'badcoffee',
        'revision': 'aaaabbbb',
      })

    if url == URL_PREFIX + 'config/deadbeef':
      raise ndb.Return({
        'content':  base64.b64encode('a config'),
      })
    if url == URL_PREFIX + 'config/badcoffee':
      raise ndb.Return({
        'content':  base64.b64encode('param: "qux"'),
      })

    if url == URL_PREFIX + 'projects':
      raise ndb.Return({
        'projects': [
          {
           'id': 'chromium',
           'repo_type': 'GITILES',
           'repo_url': 'https://chromium.googlesource.com/chromium/src',
           'name': 'Chromium browser'
          },
          {
           'id': 'infra',