def test_create_api_user(self):
     self.insert_user("bar", developer=False)
     ads_user = user.AdsUser.from_id("bar_cookie_id")
     self.assertTrue(ads_user.user_rec.developer_key in [None, ""])
     api_user = create_api_user(ads_user, "basic")
     self.assertTrue(api_user.is_developer())
     self.assertEqual(api_user.user_rec.developer_perms, PERMISSION_LEVELS["basic"])
     dev_key = api_user.get_dev_key()
     api_user2 = AdsApiUser.from_dev_key(dev_key)
     self.assertEqual(api_user.get_dev_key(), api_user2.get_dev_key()) 
Exemple #2
0
def useradd(email=None, level=None):
    """add an api user"""

    from adsabs.modules.api import api_user
    from adsabs.modules.user import AdsUser
    
    if not email:
        email = prompt("Enter e-mail address of Classic ADS account", "")
        if not len(email):
            sys.exit(1)
    if not level:
        level = prompt_choices("Enter developer permission level", sorted(api_user.PERMISSION_LEVELS.keys()), "basic")
        if level not in api_user.PERMISSION_LEVELS:
            sys.exit(1)
            
    user = AdsUser.from_email(email)
    if not user:
        app.logger.info("user not found")
        sys.exit(1)
        
    # first check if the user is already a dev
    user = api_user.AdsApiUser(user.user_rec)
    if user.is_developer():
        app.logger.info("User already has api access. Developer key: %s" % user.get_dev_key())
        if prompt_bool("Would you like to reset the user's permissions", False):
            user.set_perms(level)
            app.logger.info("API User permissions updated")
        return
            
    user = api_user.create_api_user(user, level)
    dev_key = user.get_dev_key()
    app.logger.info("API User created with %s permissions. Developer key: %s" % (level, dev_key))
    
    if prompt_bool("Send welcome message", True):
        sendwelcome(dev_key=dev_key, no_prompt=True)

    if prompt_bool("Update Google Spreadsheet", True):
        update_spreadsheet(user=user)