From 6218021defd79c094799739197b646058cd5cd3e Mon Sep 17 00:00:00 2001 From: kpiyush04 <04kpiyush@gmail.com> Date: Mon, 31 May 2021 18:04:06 +0530 Subject: [PATCH] Incresed Code Coverage --- DESCRIPTION | 3 +- NAMESPACE | 1 + R/api.R | 1 + tests/fixtures/check_api_version.yml | 33 ++++++++++++ tests/fixtures/check_authentication.yml | 39 ++++++++++++++ tests/fixtures/check_last_audit.yml | 35 ++++++++++++ tests/fixtures/create_bugzilla_key.yml | 71 +++++++++++++++++++++++++ tests/fixtures/valid_key.yml | 1 + tests/testthat/setup-bugRzilla.R | 59 ++++++++++++++++++++ 9 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/check_api_version.yml create mode 100644 tests/fixtures/check_authentication.yml create mode 100644 tests/fixtures/check_last_audit.yml create mode 100644 tests/fixtures/create_bugzilla_key.yml create mode 100644 tests/fixtures/valid_key.yml diff --git a/DESCRIPTION b/DESCRIPTION index ca85fd9..e09841e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,7 +17,8 @@ Imports: httr, methods, utils, - tools + tools, + httptest Suggests: covr, knitr, diff --git a/NAMESPACE b/NAMESPACE index 8fb5035..c05d995 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ export(get_history) export(post_bug) export(post_comment) export(set_key) +import(httptest) importFrom(cli,cat_rule) importFrom(cli,cli_alert) importFrom(cli,cli_alert_danger) diff --git a/R/api.R b/R/api.R index 800fb98..5795bca 100644 --- a/R/api.R +++ b/R/api.R @@ -17,6 +17,7 @@ missing_key <- function(key) { #' @importFrom cli cli_ul #' @importFrom utils browseURL #' @export +#' @import httptest create_bugzilla_key <- function(host) { host <- missing_host(host) url <- paste0(host, "userprefs.cgi?tab=apikey") diff --git a/tests/fixtures/check_api_version.yml b/tests/fixtures/check_api_version.yml new file mode 100644 index 0000000..f34166b --- /dev/null +++ b/tests/fixtures/check_api_version.yml @@ -0,0 +1,33 @@ +http_interactions: +- request: + method: get + uri: https://bugs.r-project.org/bugzilla/rest/version + body: + encoding: '' + string: '' + headers: + Accept: application/json, text/xml, application/xml, */* + response: + status: + status_code: 200 + category: Success + reason: OK + message: 'Success: (200) OK' + headers: + date: Mon, 31 May 2021 03:18:02 GMT + server: gazelle + etag: eTsYwUQ9/dEzCleTaxjJ5A + content-length: '20' + content-type: application/json; charset=UTF-8 + access-control-allow-headers: accept, content-type, origin, user-agent, x-requested-with, + x-bugzilla-api-key, x-bugzilla-password, x-bugzilla-token, x-bugzilla-login + access-control-allow-origin: '*' + x-content-type-options: nosniff + x-frame-options: SAMEORIGIN + x-xss-protection: 1; mode=block + body: + encoding: UTF-8 + file: no + string: '{"version":"5.1.2+"}' + recorded_at: 2021-05-31 03:18:02 GMT + recorded_with: vcr/0.6.0, webmockr/0.8.0 diff --git a/tests/fixtures/check_authentication.yml b/tests/fixtures/check_authentication.yml new file mode 100644 index 0000000..ef8177e --- /dev/null +++ b/tests/fixtures/check_authentication.yml @@ -0,0 +1,39 @@ +http_interactions: +- request: + method: get + uri: https://bugs.r-project.org/bugzilla/rest/whoami + body: + encoding: '' + string: '' + headers: + Accept: application/json, text/xml, application/xml, */* + X-BUGZILLA-API-KEY: https://bugs.r-project.org/bugzilla/ + User-agent: https://github.com/llrs/bugRzilla/ + response: + status: + status_code: 400 + category: Client error + reason: Bad Request + message: 'Client error: (400) Bad Request' + headers: + date: Mon, 31 May 2021 03:18:01 GMT + server: gazelle + etag: QoEOn1fkyvtzNJvFp167rw + content-length: '185' + content-type: application/json; charset=UTF-8 + access-control-allow-headers: accept, content-type, origin, user-agent, x-requested-with, + x-bugzilla-login, x-bugzilla-token, x-bugzilla-password, x-bugzilla-api-key + access-control-allow-origin: '*' + x-content-type-options: nosniff + x-frame-options: SAMEORIGIN + x-xss-protection: 1; mode=block + set-cookie: Bugzilla_login_request_cookie=MGh4nMQZ13; domain=bugs.r-project.org; + path=/bugzilla/; secure; HttpOnly + connection: close + body: + encoding: UTF-8 + file: no + string: '{"code":"306","error":true,"message":"The API key you specified is + invalid. Please check that you typed it correctly.","documentation":"https://bugzilla.readthedocs.org/en/latest/api/"}' + recorded_at: 2021-05-31 03:18:00 GMT + recorded_with: vcr/0.6.0, webmockr/0.8.0 diff --git a/tests/fixtures/check_last_audit.yml b/tests/fixtures/check_last_audit.yml new file mode 100644 index 0000000..358ca0f --- /dev/null +++ b/tests/fixtures/check_last_audit.yml @@ -0,0 +1,35 @@ +http_interactions: +- request: + method: get + uri: https://bugs.r-project.org/bugzilla/rest/last_audit_time + body: + encoding: '' + string: '' + headers: + Accept: application/json, text/xml, application/xml, */* + response: + status: + status_code: 200 + category: Success + reason: OK + message: 'Success: (200) OK' + headers: + date: Mon, 31 May 2021 03:04:29 GMT + server: gazelle + etag: GIrwjX1k17GX4FOvl+NcVA + content-length: '42' + content-type: application/json; charset=UTF-8 + access-control-allow-headers: accept, content-type, origin, user-agent, x-requested-with, + x-bugzilla-login, x-bugzilla-password, x-bugzilla-token, x-bugzilla-api-key + access-control-allow-origin: '*' + x-content-type-options: nosniff + x-frame-options: SAMEORIGIN + x-xss-protection: 1; mode=block + set-cookie: Bugzilla_login_request_cookie=l0ZOvJZzmk; domain=bugs.r-project.org; + path=/bugzilla/; secure; HttpOnly + body: + encoding: UTF-8 + file: no + string: '{"last_audit_time":"2021-05-27T01:19:10Z"}' + recorded_at: 2021-05-31 03:04:28 GMT + recorded_with: vcr/0.6.0, webmockr/0.8.0 diff --git a/tests/fixtures/create_bugzilla_key.yml b/tests/fixtures/create_bugzilla_key.yml new file mode 100644 index 0000000..5d53183 --- /dev/null +++ b/tests/fixtures/create_bugzilla_key.yml @@ -0,0 +1,71 @@ +http_interactions: +- request: + method: get + uri: https://bugs.r-project.org/bugzilla/rest/version + body: + encoding: '' + string: '' + headers: + Accept: application/json, text/xml, application/xml, */* + response: + status: + status_code: 200 + category: Success + reason: OK + message: 'Success: (200) OK' + headers: + date: Sun, 30 May 2021 22:29:14 GMT + server: gazelle + etag: eTsYwUQ9/dEzCleTaxjJ5A + content-length: '20' + content-type: application/json; charset=UTF-8 + access-control-allow-headers: accept, content-type, origin, user-agent, x-requested-with, + x-bugzilla-login, x-bugzilla-token, x-bugzilla-password, x-bugzilla-api-key + access-control-allow-origin: '*' + x-content-type-options: nosniff + x-frame-options: SAMEORIGIN + x-xss-protection: 1; mode=block + set-cookie: Bugzilla_login_request_cookie=bkWtzcH9Y2; domain=bugs.r-project.org; + path=/bugzilla/; secure; HttpOnly + body: + encoding: UTF-8 + file: no + string: '{"version":"5.1.2+"}' + recorded_at: 2021-05-30 22:29:15 GMT + recorded_with: vcr/0.6.0, webmockr/0.8.0 +- request: + method: get + uri: https://bugs.r-project.org/bugzilla/rest/whoami + body: + encoding: '' + string: '' + headers: + Accept: application/json, text/xml, application/xml, */* + X-BUGZILLA-API-KEY: foobar + User-agent: https://github.com/llrs/bugRzilla/ + response: + status: + status_code: 400 + category: Client error + reason: Bad Request + message: 'Client error: (400) Bad Request' + headers: + date: Sun, 30 May 2021 22:29:14 GMT + server: gazelle + etag: QoEOn1fkyvtzNJvFp167rw + content-length: '185' + content-type: application/json; charset=UTF-8 + access-control-allow-headers: accept, content-type, origin, user-agent, x-requested-with, + x-bugzilla-api-key, x-bugzilla-password, x-bugzilla-token, x-bugzilla-login + access-control-allow-origin: '*' + x-content-type-options: nosniff + x-frame-options: SAMEORIGIN + x-xss-protection: 1; mode=block + connection: close + body: + encoding: UTF-8 + file: no + string: '{"documentation":"https://bugzilla.readthedocs.org/en/latest/api/","message":"The + API key you specified is invalid. Please check that you typed it correctly.","error":true,"code":"306"}' + recorded_at: 2021-05-30 22:29:15 GMT + recorded_with: vcr/0.6.0, webmockr/0.8.0 diff --git a/tests/fixtures/valid_key.yml b/tests/fixtures/valid_key.yml new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tests/fixtures/valid_key.yml @@ -0,0 +1 @@ + diff --git a/tests/testthat/setup-bugRzilla.R b/tests/testthat/setup-bugRzilla.R index e56a6b7..a48750b 100644 --- a/tests/testthat/setup-bugRzilla.R +++ b/tests/testthat/setup-bugRzilla.R @@ -1,4 +1,5 @@ library("vcr") # *Required* as vcr is set up on loading +library("httptest") vcr_dir <- vcr::vcr_test_path("fixtures") if (!nzchar(Sys.getenv("R_BUGZILLA"))) { @@ -12,6 +13,52 @@ if (!nzchar(Sys.getenv("R_BUGZILLA"))) { use_key() +test_that("create_bugzilla_key works", { + vcr::use_cassette("create_bugzilla_key", { + host <- "https://bugs.r-project.org/bugzilla/" + cbk <- create_bugzilla_key(host) + }) + expect_equal(a <- paste0(host, "userprefs.cgi?tab=apikey"), url) + cli::test_that_cli("cli_alert_warning works", { + expect_snapshot({ + cli::cli_alert_warning("This package hasn't been tested with this Bugzilla version.") + }) + }) +}) + + +test_that("check_authentication works", { + vcr::use_cassette("check_authentication", { + ca <- check_authentication(host) + }) + expect_equal(missing_host(host), "https://bugs.r-project.org/bugzilla/") + cli::test_that_cli("cli_alert_danger",{ + expect_snapshot({ + cli::cli_alert_danger("Not authenticated on this site.") + cli::cli_alert_success("Authenticated on this site!") + }) + }) +}) + +test_that("check_api_version works", { + vcr::use_cassette("check_api_version", { + cav <- check_api_version(host) + }) + expect_equal(missing_host(host), "https://bugs.r-project.org/bugzilla/") +}) + + +test_that("check_last_audit works", { + vcr::use_cassette("check_last_audit", { + product <- "R" + cla <- check_last_audit(product, host) + }) + expect_equal(missing_host(host), "https://bugs.r-project.org/bugzilla/") + httptest::expect_GET(paste0(host, "rest/last_audit_time"), "https://bugs.r-project.org/bugzilla/rest/last_audit_time") + expect_equal(missing_product(product), "R") +}) + + invisible(vcr::vcr_configure( dir = vcr_dir, filter_request_headers = list( @@ -19,3 +66,15 @@ invisible(vcr::vcr_configure( "X-BUGZILLA-API-KEY" = "Removing this header too just in case") )) vcr::check_cassette_names() + + +test_that("valid_key works", { + use_cassette("valid_key", { + vk <- valid_key("R_BUGZILLA") + }) + cli::test_that_cli("cli_alert_danger", { + expect_snapshot({ + cli::cli_alert_danger("Key not valid or not set.") + }) + }) +})