def test_create_user_are_different():

    user1 = factories.User()
    user2 = factories.User()
    assert user1.id != user2.id

    assert user1.created != user2.created
Exemple #2
0
def test_get_user_buckets_two_users():

    user1 = factories.User()
    factories.User()

    factories.S3Bucket(name='test-bucket-1', users=[user1])
    factories.S3Bucket(name='test-bucket-2')

    buckets = get_user_buckets(user1.id)

    assert len(buckets) == 1

    assert buckets[0].name == 'test-bucket-1'
Exemple #3
0
def test_api_source_job_create_non_api_integration_source(post):
    user = factories.User()
    token = user.create_api_token()
    source = factories.Source(users=[user], integration_name='github')
    code, data = post('/api/source/%s/job' % source.id, {}, token=token.token)
    assert code == 403
    assert data['message'].startswith('Forbidden')
Exemple #4
0
def test_api_source_job_create_foreign_private_source(post):
    user = factories.User()
    token = user.create_api_token()
    source = factories.Source(users=[], integration_name='api', conf={'private': True})
    code, data = post('/api/source/%s/job' % source.id, {}, token=token.token)
    assert code == 403
    assert data['message'] == 'Forbidden'
Exemple #5
0
def test_api_source_job_get_not_found(get):
    user = factories.User()
    token = user.create_api_token()
    source = factories.Source(users=[user], integration_name='api')
    code, data = get('/api/source/%s/job/not-existent' % source.id, token=token.token)
    assert code == 404
    assert data['message'] == 'Not Found'
Exemple #6
0
def test_api_source_job_create_empty_body(post):
    user = factories.User()
    token = user.create_api_token()
    source = factories.Source(users=[user], integration_name='api')
    code, data = post('/api/source/%s/job' % source.id, {}, token=token.token)
    assert code == 400
    assert data['message'] == 'Missing configuration'
Exemple #7
0
def test_api_repo_list(client):
    user = factories.User(github_oauth_token='token')
    repo1 = factories.GithubRepo(name='name1', users=[user])
    repo2 = factories.GithubRepo(name='name2', users=[user])
    with client.session_transaction() as session:
        session['user_id'] = user.id
    response = client.get('/github/api/repo')
    assert response.status_code == 200
    assert get_response_data(response) == {
        'repos': [
            {
                'id': repo1.id,
                'name': repo1.name,
                'active': repo1.active
            },
            {
                'id': repo2.id,
                'name': repo2.name,
                'active': repo2.active
            },
        ],
        'syncing':
        False,
        'error':
        None,
    }
Exemple #8
0
def test_api_source_create_duplicate_name(post):
    user = factories.User()
    token = user.create_api_token()
    source = Source.create(name='name', integration_name='api')
    code, data = post('/api/source', {'name': 'name'}, token=token.token)
    assert code == 409
    assert data['message'] == 'Source name is in use'
def test_create_job_pr_other_action(set_commit_status, run_validation, client):

    user = factories.User(github_oauth_token='xxx')
    source = factories.GithubRepo(name='test-org/example',
                                  users=[user])

    data = json.dumps({
      'action': 'labeled',
      'pull_request': {
          'number': 3,
          'title': 'Test PR',
          'user': {
              'login': '******',
          },

          'head': {
              'repo': {'name': 'example', 'owner': {'login': '******'}},
              'sha': 'test-sha',
          },
          'base': {
              'repo': {'name': 'example', 'owner': {'login': '******'}},
          }
       },
    })
    signature = create_signature(settings.GITHUB_HOOK_SECRET, data)
    response = client.post(
        '/github/hook',
        headers={'X-Hub-Signature': signature},
        content_type='application/json',
        data=data)

    assert response.status_code == 200
    assert len(source.jobs) == 0
def test_s3_api_bucket_add(mock_set_up_bucket_on_aws, client):
    user = factories.User()
    mock_set_up_bucket_on_aws.return_value = (True, '')
    with client.session_transaction() as session:
        session['user_id'] = user.id
    response = client.post('/s3/api/bucket',
        headers={'Content-Type': 'application/json'},
        data=json.dumps({
            'access-key-id': 'id',
            'secret-access-key': 'key',
            'bucket-name': 'name',
        })
    )
    buckets = database['session'].query(S3Bucket).all()
    mock_set_up_bucket_on_aws.assert_called_with('id', 'key', 'name')
    assert buckets[0].name == 'name'
    assert buckets[0].users[0] == user
    assert get_response_data(response) == {
        'bucket': {
            'id': mock.ANY,
            'name': 'name',
            'active': True,
            'integration_name': 's3'
        },
        'error': None,
    }
Exemple #11
0
def test_api_source_get_foreign_private(get):
    user = factories.User()
    token = user.create_api_token()
    source = factories.Source(integration_name='api', conf={'private': True})
    code, data = get('/api/source/%s' % source.id, token=token.token)
    assert code == 403
    assert data['message'] == 'Forbidden'
Exemple #12
0
def test_create_job(set_commit_status, client, celery_app):

    # TODO: refactor to not use actual calls!
    user = factories.User(github_oauth_token=settings.GITHUB_API_TOKEN)
    factories.GithubRepo(name='frictionlessdata/goodtables.io-example',
                         users=[user])

    data = json.dumps({
        'repository': {
            'name': 'goodtables.io-example',
            'owner': {
                'name': 'frictionlessdata'
            },
        },
        'head_commit': {
            'id': 'd5be243487d9882d7f762e7fa04b36b900164a59'
        },
    })
    signature = create_signature(settings.GITHUB_HOOK_SECRET, data)
    response = client.post('/github/hook',
                           headers={'X-Hub-Signature': signature},
                           content_type='application/json',
                           data=data)
    job_id = get_response_data(response)['job_id']
    job = models.job.get(job_id)
    assert job['id'] == job_id
    assert job['created']
    assert job['finished']
    assert job['status'] == 'failure'
    assert job['report']
def test_s3_api_bucket_add_user_on_free_plan(client):
    user = factories.User()
    bucket1 = factories.S3Bucket()
    bucket1.users.append(user)
    bucket2 = factories.S3Bucket()
    bucket2.users.append(user)

    with client.session_transaction() as session:
        session['user_id'] = user.id
    response = client.post(
        '/s3/api/bucket',
        headers={'Content-Type': 'application/json'},
        data=json.dumps({
            'access-key-id': 'id',
            'secret-access-key': 'key',
            'bucket-name': 'name',
        })
    )

    assert response.status_code == 200
    assert get_response_data(response) == {
        'bucket': None,
        'error': 'Free plan users can only have {} active buckets'.format(
            settings.MAX_S3_BUCKETS_ON_FREE_PLAN)
    }
def test_s3_api_bucket_add_user_admin(mock_set_up_bucket_on_aws, client):

    mock_set_up_bucket_on_aws.return_value = (True, '')

    user = factories.User(admin=True)

    bucket1 = factories.S3Bucket()
    bucket1.users.append(user)
    bucket2 = factories.S3Bucket()
    bucket2.users.append(user)

    with client.session_transaction() as session:
        session['user_id'] = user.id
    response = client.post(
        '/s3/api/bucket',
        headers={'Content-Type': 'application/json'},
        data=json.dumps({
            'access-key-id': 'id',
            'secret-access-key': 'key',
            'bucket-name': 'name',
        })
    )

    assert response.status_code == 200
    assert get_response_data(response) == {
        'bucket': {
            'id': mock.ANY,
            'name': 'name',
            'active': True,
            'integration_name': 's3'
        },
        'error': None,
    }
def test_create_job_pr(set_commit_status, run_validation, client):

    user = factories.User(github_oauth_token='xxx')
    factories.GithubRepo(name='test-org/example',
                         users=[user])

    data = json.dumps({
      'action': 'opened',
      'pull_request': {
          'number': 3,
          'title': 'Test PR',
          'user': {
              'login': '******',
          },

          'head': {
              'repo': {'name': 'example', 'owner': {'login': '******'}},
              'sha': 'test-sha',
          },
          'base': {
              'repo': {'name': 'example', 'owner': {'login': '******'}},
          }
       },
    })
    signature = create_signature(settings.GITHUB_HOOK_SECRET, data)
    response = client.post(
        '/github/hook',
        headers={'X-Hub-Signature': signature},
        content_type='application/json',
        data=data)
    job_id = get_response_data(response)['job_id']
    job = models.job.get(job_id)
    assert job['id'] == job_id
    assert job['created']
    assert job['status'] == 'created'
def test_create_job_push(set_commit_status, run_validation, client):

    user = factories.User(github_oauth_token='xxx')
    factories.GithubRepo(name='test-org/example',
                         users=[user])

    data = json.dumps({
        'ref': 'refs/head/some-branch',
        'repository': {
            'name': 'example',
            'owner': {'name': 'test-org'},
        },
        'head_commit': {
            'id': 'xxx',
            'message': 'Test commit', 'author': {'username': '******'}},
    })
    signature = create_signature(settings.GITHUB_HOOK_SECRET, data)
    response = client.post(
        '/github/hook',
        headers={'X-Hub-Signature': signature},
        content_type='application/json',
        data=data)
    job_id = get_response_data(response)['job_id']
    job = models.job.get(job_id)
    assert job['id'] == job_id
    assert job['created']
    assert job['status'] == 'created'
Exemple #17
0
def test_user_relationship():
    user = factories.User()
    repo = GithubRepo(id='id', name='owner/repo', users=[user])
    database['session'].add(repo)
    database['session'].commit()
    repos = database['session'].query(GithubRepo).all()
    assert len(repos) == 1
    assert repos[0].users[0].id == user.id
Exemple #18
0
def test_api_source_get(get):
    user = factories.User()
    token = user.create_api_token()
    source = factories.Source(users=[user], integration_name='github')
    code, data = get('/api/source/%s' % source.id, token=token.token)
    assert code == 200
    assert data['source']['id'] == source.id
    assert data['source']['name'] == source.name
def test_create_user_overrides():
    user = factories.User(id='my-id', name='my-name', admin=True,
                          provider_ids={'github': '123'})

    assert user.id == 'my-id'
    assert user.name == 'my-name'
    assert user.admin is True
    assert user.provider_ids['github'] == '123'
def test_s3_api_bucket_deactivate_no_access_to_bucket(client):
    user = factories.User()
    bucket = factories.S3Bucket(
        conf={'access_key_id': 'test', 'secret_access_key': 'test'})
    with client.session_transaction() as session:
        session['user_id'] = user.id
    response = client.get('/s3/api/bucket/{}/deactivate'.format(bucket.id))
    assert response.status_code == 403
Exemple #21
0
def test_api_source_job_create_wrong_params(post):
    user = factories.User()
    token = user.create_api_token()
    source = factories.Source(users=[user], integration_name='api')
    payload = {'not_files': [{'source': 'http://example.com'}]}
    code, data = post('/api/source/%s/job' % source.id, payload, token=token.token)
    assert code == 400
    assert data['message'] == 'Invalid configuration'
Exemple #22
0
def test_api_source_job_get(get):
    user = factories.User()
    token = user.create_api_token()
    source = factories.Source(users=[user],
        integration_name='api', conf={'private': True})
    job = factories.Job(source=source)
    code, data = get('/api/source/%s/job/%s' % (source.id, job.id), token=token.token)
    assert code == 200
    assert data['job']['id'] == job.id
Exemple #23
0
def test_user_set_plan_no_expire():

    plan = database['session'].query(Plan).filter_by(name='free').one()
    assert plan.frequency == ''

    user = factories.User()
    user.set_plan(plan.name)

    assert user.subscription.expires is None
Exemple #24
0
def test_api_token_delete(delete):
    user = factories.User()
    token1 = user.create_api_token()
    token2 = user.create_api_token()
    with delete.client.session_transaction() as session:
        session['user_id'] = user.id
    code, data = delete('/api/token/%s' % token2.id)
    assert code == 200
    assert user.api_tokens == [token1]
Exemple #25
0
def test_api_source_get_own_private(get):
    user = factories.User()
    token = user.create_api_token()
    source = factories.Source(users=[user],
        integration_name='api', conf={'private': True})
    code, data = get('/api/source/%s' % source.id, token=token.token)
    assert code == 200
    assert data['source']['id'] == source.id
    assert data['source']['name'] == source.name
Exemple #26
0
def test_api_source_job_list(get):
    source = factories.Source(integration_name='api')
    job1 = factories.Job(source=source)
    job2 = factories.Job(source=source)
    user = factories.User()
    token = user.create_api_token()
    code, data = get('/api/source/%s/job' % source.id, token=token.token)
    assert code == 200
    assert data['jobs'][0]['id'] == job2.id
    assert data['jobs'][1]['id'] == job1.id
Exemple #27
0
def test_get_user_buckets_count():

    user = factories.User()

    factories.S3Bucket(name='test-bucket-1', users=[user])
    factories.S3Bucket(name='test-bucket-2', users=[user])

    buckets_count = get_user_buckets_count(user.id)

    assert buckets_count == 2
Exemple #28
0
def test_api_source_job_create(post):
    user = factories.User()
    token = user.create_api_token()
    source = factories.Source(users=[user], integration_name='api')
    payload = {'source': [{'source': 'http://example.com'}]}
    with mock.patch('goodtablesio.tasks.validate'):
        code, data = post('/api/source/%s/job' % source.id, payload, token=token.token)
    job = Job.get(data['job']['id'])
    assert code == 200
    assert job.source == source
Exemple #29
0
def test_api_token_create(post):
    user = factories.User()
    with post.client.session_transaction() as session:
        session['user_id'] = user.id
    code, data = post('/api/token', {'description': 'description'})
    assert code == 200
    assert data['token']['id'] == user.api_tokens[0].id
    assert data['token']['token'] == user.api_tokens[0].token
    assert code == 200
    assert len(user.api_tokens) == 1
def test_create_user():
    user = factories.User()

    assert user.name
    assert user.display_name
    assert user.email
    assert user.admin is False

    user_db = database['session'].query(User).get(user.id)

    assert user_db.id == user.id