Skip to content

Commit

Permalink
add integration tests votes (#1578)
Browse files Browse the repository at this point in the history
### Feature or Bugfix
<!-- please choose -->
- Feature

### Detail
- Add integration tests for `vote` module
- Fix Vote creation by username


### Relates
- #1220

### Security
Please answer the questions below briefly where applicable, or write
`N/A`. Based on
[OWASP 10](https://owasp.org/Top10/en/).

- Does this PR introduce or modify any input fields or queries - this
includes
fetching data from storage outside the application (e.g. a database, an
S3 bucket)?
  - Is the input sanitized?
- What precautions are you taking before deserializing the data you
consume?
  - Is injection prevented by parametrizing queries?
  - Have you ensured no `eval` or similar functions are used?
- Does this PR introduce any functionality or component that requires
authorization?
- How have you ensured it respects the existing AuthN/AuthZ mechanisms?
  - Are you logging failed auth attempts?
- Are you using or adding any cryptographic features?
  - Do you use a standard proven implementations?
  - Are the used keys controlled by the customer? Where are they stored?
- Are you introducing any new policies/roles/users?
  - Have you used the least-privilege principle? How?


By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.
  • Loading branch information
noah-paige authored Oct 1, 2024
1 parent cb909e9 commit 0931949
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/dataall/modules/vote/db/vote_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def get_vote(session, targetUri, targetType) -> [models.Vote]:
.filter(
models.Vote.targetUri == targetUri,
models.Vote.targetType == targetType,
models.Vote.username == get_context().username,
)
.first()
)
Expand All @@ -24,6 +25,7 @@ def upvote(session, targetUri: str, targetType: str, upvote: bool) -> [models.Vo
vote: models.Vote = (
session.query(models.Vote)
.filter(
models.Vote.username == get_context().username,
models.Vote.targetUri == targetUri,
models.Vote.targetType == targetType,
)
Expand Down
11 changes: 11 additions & 0 deletions tests_new/integration_tests/modules/vote/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest
from integration_tests.modules.vote.queries import upvote, get_vote


S3_DATASET_TARGET_TYPE = 'dataset'


@pytest.fixture(scope='session')
def vote1(client1, session_s3_dataset1):
upvote(client1, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, True)
yield get_vote(client1, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE)
55 changes: 55 additions & 0 deletions tests_new/integration_tests/modules/vote/queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# TODO: This file will be replaced by using the SDK directly


def upvote(client, uri, target_type, vote):
query = {
'operationName': 'upVote',
'variables': {'input': {'targetUri': uri, 'targetType': target_type, 'upvote': vote}},
'query': """
mutation upVote($input:VoteInput!){
upVote(input:$input){
voteUri
targetUri
targetType
upvote
}
}
""",
}

response = client.query(query=query)
return response.data.upVote


def count_upvotes(client, uri, target_type):
query = {
'operationName': 'countUpVotes',
'variables': {'targetUri': uri, 'targetType': target_type},
'query': """
query countUpVotes($targetUri:String!, $targetType:String!){
countUpVotes(targetUri:$targetUri, targetType:$targetType)
}
""",
}
response = client.query(query=query)
return response.data.countUpVotes


def get_vote(client, uri, target_type):
query = {
'operationName': 'getVote',
'variables': {'targetUri': uri, 'targetType': target_type},
'query': """
query getVote($targetUri:String!, $targetType:String!){
getVote(targetUri:$targetUri, targetType:$targetType){
upvote
voteUri
targetUri
targetType
}
}
""",
}
response = client.query(query=query)
print(response.data.getVote)
return response.data.getVote
51 changes: 51 additions & 0 deletions tests_new/integration_tests/modules/vote/test_vote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from assertpy import assert_that

from integration_tests.errors import GqlError

from integration_tests.modules.vote.queries import upvote, count_upvotes, get_vote
from integration_tests.modules.vote.conftest import S3_DATASET_TARGET_TYPE


def test_upvote(client1, vote1):
assert_that(vote1).is_not_none()
assert_that(vote1.voteUri).is_not_none()
assert_that(vote1.upvote).is_true()


def test_upvote_invalid(client1, vote1, session_s3_dataset1):
assert_that(upvote).raises(GqlError).when_called_with(client1, session_s3_dataset1.datasetUri, None, True).contains(
'targetType', 'not to be None'
)
assert_that(upvote).raises(GqlError).when_called_with(client1, None, S3_DATASET_TARGET_TYPE, True).contains(
'targetUri', 'not to be None'
)


def test_get_vote_invalid(client1, vote1, session_s3_dataset1):
assert_that(get_vote).raises(GqlError).when_called_with(client1, session_s3_dataset1.datasetUri, None).contains(
'targetType', 'must not be null'
)
assert_that(get_vote).raises(GqlError).when_called_with(client1, None, S3_DATASET_TARGET_TYPE).contains(
'targetUri', 'must not be null'
)


def test_count_upvote_invalid(client1, vote1, session_s3_dataset1):
assert_that(count_upvotes).raises(GqlError).when_called_with(
client1, session_s3_dataset1.datasetUri, None
).contains('targetType', 'must not be null')
assert_that(count_upvotes).raises(GqlError).when_called_with(client1, None, S3_DATASET_TARGET_TYPE).contains(
'targetUri', 'must not be null'
)


def test_count_votes(client2, vote1, session_s3_dataset1):
count = count_upvotes(client2, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE)

# Assert incremeent by 1
upvote(client2, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, True)
assert_that(count_upvotes(client2, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE)).is_equal_to(count + 1)

# Assert decrement by 1
upvote(client2, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, False)
assert_that(count_upvotes(client2, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE)).is_equal_to(count)

0 comments on commit 0931949

Please sign in to comment.