Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22 create test cases #24

Merged
merged 4 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Package: Rcognito
Package: RCognito
Type: Package
Title: A Simplified Interface for Amazon Cognito
Version: 0.1.0
Expand All @@ -11,3 +11,6 @@ Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.2
Imports: paws
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

export(change_password)
export(confirm_forgot_password)
export(confirm_sign_up)
export(confirm_sign_up_user)
export(forgot_password)
export(sign_in)
export(sign_up)
export(sign_up_user)
import(paws)
6 changes: 3 additions & 3 deletions R/confirm_sign_up.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @return Nothing (displays a message on success or raises an error if it fails).
#' @export

confirm_sign_up <- function(client_id,
confirm_sign_up_user <- function(client_id,
userpool,
username,
verification_code) {
Expand All @@ -24,8 +24,8 @@ confirm_sign_up <- function(client_id,
userpool)

base::stopifnot(
"Duplicate username, Please enter another username" =
!username %in% users_info_from_cognito$usernames
"User not found, Please enter correct username" =
username %in% users_info_from_cognito$usernames
)

cognito$confirm_sign_up(
Expand Down
3 changes: 2 additions & 1 deletion R/sign_in.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#' @return A list with user information and tokens if successful; throws an error otherwise.
#' @export

sign_in <- function(client_id, username, password, authflow = "USER_PASSWORD_AUTH") {
sign_in <- function(client_id, username, password,
authflow = "USER_PASSWORD_AUTH") {
tryCatch({

cognito <- paws::cognitoidentityprovider()
Expand Down
2 changes: 1 addition & 1 deletion R/sign_up.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @return A list with sign-up confirmation details if successful; throws an error otherwise.
#' @export

sign_up <- function(client_id, email, username, password, ...) {
sign_up_user <- function(client_id, email, username, password, ...) {

user_attributes <- list(
list(Name = "email", Value = email),
Expand Down
6 changes: 3 additions & 3 deletions man/confirm_sign_up.Rd → man/confirm_sign_up_user.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/sign_up.Rd → man/sign_up_user.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(Rcognito)

test_check("Rcognito")
Empty file.
Empty file.
45 changes: 45 additions & 0 deletions tests/testthat/test-confirm_sign_up.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
library(testthat)
library(mockery)

testthat::test_that("confirm_sign_up_user function works with mocked AWS
Cognito response", {

mock_user_list <- mockery::mock(
list(usernames = c("existinguser", "johndoe")),
cycle = TRUE)

mockery::stub(confirm_sign_up_user,
"get_user_list_from_cognito",
mock_user_list)

mock_cognito <- mockery::mock(list(
confirm_sign_up = function(ClientId, Username, ConfirmationCode) {
list()
}
), cycle = TRUE)

mockery::stub(confirm_sign_up_user,
"paws::cognitoidentityprovider",
mock_cognito)

testthat::expect_message(
confirm_sign_up_user(
client_id = "mock_client_id",
userpool = "mock_userpool",
username = "johndoe",
verification_code = "123456"
)
)


testthat::expect_error(
confirm_sign_up_user(
client_id = "mock_client_id",
userpool = "mock_userpool",
username = "nonexistentuser",
verification_code = "123456"
),
"User not found, Please enter correct username"
)

})
Empty file.
55 changes: 55 additions & 0 deletions tests/testthat/test-sign_in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
library(testthat)
library(mockery)

testthat::test_that("sign_in function works with mocked AWS Cognito response", {

mock_cognito <- mockery::mock(list(
initiate_auth = function(AuthFlow, AuthParameters, ClientId) {

list(
AuthenticationResult = list(
AccessToken = "mock_access_token",
IdToken = "mock_id_token",
RefreshToken = "mock_refresh_token"
)
)
}
))

mockery::stub(sign_in, "paws::cognitoidentityprovider", mock_cognito)

result <- sign_in(
client_id = "mock_client_id",
username = "johndoe",
password = "mock_password123"
)

# Assertions to verify the expected behavior
testthat::expect_type(result, "list")
testthat::expect_true("AuthenticationResult" %in% names(result))
testthat::expect_equal(result$AuthenticationResult$AccessToken, "mock_access_token")
testthat::expect_equal(result$AuthenticationResult$IdToken, "mock_id_token")
testthat::expect_equal(result$AuthenticationResult$RefreshToken, "mock_refresh_token")
})

testthat::test_that("sign_in function handles failure with mocked
AWS Cognito response", {

mock_cognito <- mockery::mock(list(
initiate_auth = function(AuthFlow, AuthParameters, ClientId) {
stop("NotAuthorizedException: Incorrect username or password.")
}
))

mockery::stub(sign_in, "paws::cognitoidentityprovider", mock_cognito)

testthat::expect_error(
sign_in(
client_id = "mock_client_id",
username = "wronguser",
password = "wrong_password"
),
"Failed to sign in: NotAuthorizedException: Incorrect username or password."
)
})

32 changes: 32 additions & 0 deletions tests/testthat/test-sign_up.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
library(testthat)
library(mockery)

testthat::test_that("sign_up_user function works with mocked AWS Cognito response", {
mock_cognito <- mockery::mock(list(
sign_up = function(ClientId, Username, Password, UserAttributes) {
list(
UserConfirmed = FALSE,
CodeDeliveryDetails = list(
AttributeName = "email",
DeliveryMedium = "EMAIL",
Destination = "j***@example.com"
)
)
}
))

mockery::stub(sign_up_user, "paws::cognitoidentityprovider", mock_cognito)

result <- sign_up_user(
client_id = "mock_client_id",
email = "[email protected]",
username = "johndoe",
password = "mock_password123"
)

testthat::expect_type(result, "list")
testthat::expect_false(result$UserConfirmed)
testthat::expect_equal(result$CodeDeliveryDetails$AttributeName, "email")
testthat::expect_equal(result$CodeDeliveryDetails$DeliveryMedium, "EMAIL")
})

Empty file added tests/testthat/test-utils.R
Empty file.