From 4c585247bec5fcd6fb89240a9ef89b108b099ac1 Mon Sep 17 00:00:00 2001 From: shettysanjay01 Date: Sun, 10 Nov 2024 11:29:38 +0530 Subject: [PATCH 1/4] changing Package Name --- DESCRIPTION | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 654954a..529b7e4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,4 +1,4 @@ -Package: Rcognito +Package: RCognito Type: Package Title: A Simplified Interface for Amazon Cognito Version: 0.1.0 @@ -11,3 +11,6 @@ Encoding: UTF-8 LazyData: true RoxygenNote: 7.3.2 Imports: paws +Suggests: + testthat (>= 3.0.0) +Config/testthat/edition: 3 From 9231a9260e5cb797398ab5c2292fcfcd9b72386c Mon Sep 17 00:00:00 2001 From: shettysanjay01 Date: Sun, 10 Nov 2024 17:44:30 +0530 Subject: [PATCH 2/4] function name change --- NAMESPACE | 4 ++-- R/confirm_sign_up.R | 6 +++--- R/sign_in.R | 3 ++- R/sign_up.R | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 55a7862..919022e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/confirm_sign_up.R b/R/confirm_sign_up.R index 2892e20..0390a15 100644 --- a/R/confirm_sign_up.R +++ b/R/confirm_sign_up.R @@ -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) { @@ -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( diff --git a/R/sign_in.R b/R/sign_in.R index 226a749..92f202e 100644 --- a/R/sign_in.R +++ b/R/sign_in.R @@ -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() diff --git a/R/sign_up.R b/R/sign_up.R index 2054f44..ea01425 100644 --- a/R/sign_up.R +++ b/R/sign_up.R @@ -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), From 37b4f153214f0d0e4dd2c05d0c4b3d424970af10 Mon Sep 17 00:00:00 2001 From: shettysanjay01 Date: Sun, 10 Nov 2024 17:45:25 +0530 Subject: [PATCH 3/4] function documentation after name chnage --- man/{confirm_sign_up.Rd => confirm_sign_up_user.Rd} | 6 +++--- man/{sign_up.Rd => sign_up_user.Rd} | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) rename man/{confirm_sign_up.Rd => confirm_sign_up_user.Rd} (82%) rename man/{sign_up.Rd => sign_up_user.Rd} (86%) diff --git a/man/confirm_sign_up.Rd b/man/confirm_sign_up_user.Rd similarity index 82% rename from man/confirm_sign_up.Rd rename to man/confirm_sign_up_user.Rd index f92a4a3..655f399 100644 --- a/man/confirm_sign_up.Rd +++ b/man/confirm_sign_up_user.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/confirm_sign_up.R -\name{confirm_sign_up} -\alias{confirm_sign_up} +\name{confirm_sign_up_user} +\alias{confirm_sign_up_user} \title{Confirm User Sign Up} \usage{ -confirm_sign_up(client_id, userpool, username, verification_code) +confirm_sign_up_user(client_id, userpool, username, verification_code) } \arguments{ \item{client_id}{A character string representing the app client ID.} diff --git a/man/sign_up.Rd b/man/sign_up_user.Rd similarity index 86% rename from man/sign_up.Rd rename to man/sign_up_user.Rd index 5733292..f45d936 100644 --- a/man/sign_up.Rd +++ b/man/sign_up_user.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/sign_up.R -\name{sign_up} -\alias{sign_up} +\name{sign_up_user} +\alias{sign_up_user} \title{Sign up a new user to AWS Cognito} \usage{ -sign_up(client_id, email, username, password, ...) +sign_up_user(client_id, email, username, password, ...) } \arguments{ \item{client_id}{A character string representing the app client ID.} From 856a9a002f74a80103fac8827943d0ebe2bd1106 Mon Sep 17 00:00:00 2001 From: shettysanjay01 Date: Sun, 10 Nov 2024 17:45:56 +0530 Subject: [PATCH 4/4] tests for sign up and confirm sign up and sign in --- tests/testthat.R | 12 ++++ tests/testthat/test-change_password.R | 0 tests/testthat/test-confirm_forgot_password.R | 0 tests/testthat/test-confirm_sign_up.R | 45 +++++++++++++++ tests/testthat/test-forgot_password.R | 0 tests/testthat/test-sign_in.R | 55 +++++++++++++++++++ tests/testthat/test-sign_up.R | 32 +++++++++++ tests/testthat/test-utils.R | 0 8 files changed, 144 insertions(+) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-change_password.R create mode 100644 tests/testthat/test-confirm_forgot_password.R create mode 100644 tests/testthat/test-confirm_sign_up.R create mode 100644 tests/testthat/test-forgot_password.R create mode 100644 tests/testthat/test-sign_in.R create mode 100644 tests/testthat/test-sign_up.R create mode 100644 tests/testthat/test-utils.R diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..fe59b48 --- /dev/null +++ b/tests/testthat.R @@ -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") diff --git a/tests/testthat/test-change_password.R b/tests/testthat/test-change_password.R new file mode 100644 index 0000000..e69de29 diff --git a/tests/testthat/test-confirm_forgot_password.R b/tests/testthat/test-confirm_forgot_password.R new file mode 100644 index 0000000..e69de29 diff --git a/tests/testthat/test-confirm_sign_up.R b/tests/testthat/test-confirm_sign_up.R new file mode 100644 index 0000000..c20b58c --- /dev/null +++ b/tests/testthat/test-confirm_sign_up.R @@ -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" + ) + +}) diff --git a/tests/testthat/test-forgot_password.R b/tests/testthat/test-forgot_password.R new file mode 100644 index 0000000..e69de29 diff --git a/tests/testthat/test-sign_in.R b/tests/testthat/test-sign_in.R new file mode 100644 index 0000000..2992065 --- /dev/null +++ b/tests/testthat/test-sign_in.R @@ -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." + ) +}) + diff --git a/tests/testthat/test-sign_up.R b/tests/testthat/test-sign_up.R new file mode 100644 index 0000000..96048fe --- /dev/null +++ b/tests/testthat/test-sign_up.R @@ -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 = "johndoe@example.com", + 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") +}) + diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R new file mode 100644 index 0000000..e69de29