#!/usr/bin/env python

"""Tests admin-related functionality"""

import os
from contextlib import contextmanager

import pytest

import python_pachyderm
from tests import util


@contextmanager
def sandbox():
    client = python_pachyderm.Client()
    client.activate_enterprise(os.environ["PACH_PYTHON_ENTERPRISE_CODE"])
    root_auth_token = None
    
    try:
        root_auth_token = client.activate_auth()
        client.auth_token = root_auth_token
        try:
            yield client
        finally:
            try:
                client.deactivate_auth()
                client.auth_token = None
            except:
                print("an exception occurred trying to deactivate auth, please manually disable auth with the root auth token: {}".format(root_auth_token))
                raise
    finally:
        client.deactivate_enterprise()

@util.skip_if_no_enterprise()
def test_auth_configuration():
    with sandbox() as client:
        config = client.get_auth_configuration()
        client.set_auth_configuration(config)

@util.skip_if_no_enterprise()
def test_admins():
    with sandbox() as client:
        users = client.get_admins()
        assert users == ["github:"]
        client.modify_admins(add=["github:foobar"])
        users = client.get_admins()
        assert set(users) == set(["github:", "github:foobar"])

@util.skip_if_no_enterprise()
def test_authenticate_github():
    with sandbox() as client:
        client.authenticate_github("foobar")

@util.skip_if_no_enterprise()
def test_one_time_password():
    with sandbox() as client:
        otp = client.get_one_time_password()
        client.authenticate_one_time_password(otp)

@util.skip_if_no_enterprise()
def test_authorize():
    with sandbox() as client:
        assert client.authorize("foobar", python_pachyderm.Scope.READER)

@util.skip_if_no_enterprise()
def test_who_am_i():
    with sandbox() as client:
        i = client.who_am_i()
        assert i.username == "github:"
        assert i.is_admin

@util.skip_if_no_enterprise()
def test_scope():
    with sandbox() as client:
        repo = util.create_test_repo(client, "test_scope")
        scopes = client.get_scope("github:", repo)
        assert all(s == python_pachyderm.Scope.NONE for s in scopes)
        client.set_scope("github:", repo, python_pachyderm.Scope.READER)
        scopes = client.get_scope("github:", repo)
        assert all(s == python_pachyderm.Scope.NONE for s in scopes)

@util.skip_if_no_enterprise()
def test_acl():
    def verify_acl(client, repo):
        acl = client.get_acl(repo)
        assert len(acl.entries) == 1
        assert len(acl.robot_entries) == 0
        assert acl.entries[0].username == "github:"
        assert acl.entries[0].scope == python_pachyderm.Scope.OWNER
        return acl

    with sandbox() as client:
        repo = util.create_test_repo(client, "test_acl")
        acl = verify_acl(client, repo)
        client.set_acl(repo, acl.entries)
        verify_acl(client, repo)

@util.skip_if_no_enterprise()
def test_auth_token():
    with sandbox() as client:
        auth_token = client.get_auth_token(ttl=30)
        assert auth_token.subject == "github:"
        client.extend_auth_token(auth_token.token, 60)
        client.revoke_auth_token(auth_token.token)
        with pytest.raises(python_pachyderm.RpcError):
            client.extend_auth_token(auth_token.token, 60)

@util.skip_if_no_enterprise()
def test_groups():
    with sandbox() as client:
        assert client.get_groups() == []
        client.set_groups_for_user("github:", ["foogroup"])
        assert client.get_groups() == ["foogroup"]
        assert client.get_users("foogroup") == ["github:"]
        client.modify_members("foogroup", remove=["github:"])
        assert client.get_groups() == []
        assert client.get_users("foogroup") == []

@@ -394,32 +394,52 @@
 t.activate_auth(
+subject=%22robot:root%22
 )%0A        client
@@ -1197,31 +1197,34 @@
  users == %5B%22
-github:
+robot:root
 %22%5D%0A        c
@@ -1249,28 +1249,29 @@
 s(add=%5B%22
-github:fooba
+robot:someuse
 r%22%5D)%0A   
@@ -1342,31 +1342,35 @@
 t(%5B%22
-github:%22, %22github:fooba
+robot:root%22, %22robot:someuse
 r%22%5D)
@@ -1985,39 +1985,42 @@
  i.username == %22
-github:
+robot:root
 %22%0A        assert
@@ -2196,39 +2196,42 @@
 ient.get_scope(%22
-github:
+robot:root
 %22, repo)%0A       
@@ -2314,31 +2314,34 @@
 .set_scope(%22
-github:
+robot:root
 %22, repo, pyt
@@ -2403,23 +2403,26 @@
 _scope(%22
-github:
+robot:root
 %22, repo)
@@ -2726,31 +2726,34 @@
 sername == %22
-github:
+robot:root
 %22%0A        as
@@ -3204,23 +3204,26 @@
 ect == %22
-github:
+robot:root
 %22%0A      
@@ -3593,23 +3593,26 @@
 r_user(%22
-github:
+robot:root
 %22, %5B%22foo
@@ -3720,23 +3720,26 @@
 %22) == %5B%22
-github:
+robot:root
 %22%5D%0A     
@@ -3788,15 +3788,18 @@
 e=%5B%22
-github:
+robot:root
 %22%5D)%0A

Use robot users