Skip to content

Commit

Permalink
Incresed Code Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kpiyush04 committed May 31, 2021
1 parent 148632a commit 6218021
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Imports:
httr,
methods,
utils,
tools
tools,
httptest
Suggests:
covr,
knitr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
33 changes: 33 additions & 0 deletions tests/fixtures/check_api_version.yml
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions tests/fixtures/check_authentication.yml
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions tests/fixtures/check_last_audit.yml
Original file line number Diff line number Diff line change
@@ -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
71 changes: 71 additions & 0 deletions tests/fixtures/create_bugzilla_key.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/fixtures/valid_key.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

59 changes: 59 additions & 0 deletions tests/testthat/setup-bugRzilla.R
Original file line number Diff line number Diff line change
@@ -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"))) {
Expand All @@ -12,10 +13,68 @@ 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(
"Authorization" = "My bearer token is safe",
"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.")
})
})
})

0 comments on commit 6218021

Please sign in to comment.