Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Commit

Permalink
Added breached_sites
Browse files Browse the repository at this point in the history
  • Loading branch information
Steph Locke committed Mar 18, 2016
1 parent 164d295 commit 5419a30
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: HIBPwned
Title: Bindings for the HaveIBeenPwned.com data breach API
Version: 0.0.0.9001
Version: 0.0.0.9002
Authors@R: c(person("Steph", "Locke"
, email = "stephanie.g.locke@gmail.com"
, role = c("aut", "cre"))
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(HIBP_headers)
export(account_breaches)
export(breached_sites)
importFrom(httr,add_headers)
importFrom(httr,content)
importFrom(jsonlite,fromJSON)
Expand Down
24 changes: 24 additions & 0 deletions R/breached_sites.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' Get all (nonsensitive) breached sites in HIBP
#'
#' @param domain Search a specific domain
#' @param ... Optional passthrough to HIBP_headers()
#'
#' @return Data.frame containing breach details
#' @export
#'
#' @examples
#' breached_sites()
breached_sites<-function(domain=NULL,...){
if(!is.null(domain)&(
length(domain)!=1 |!inherits(domain,"character")) ) stop("Problematic domain")
URLS<-"https://haveibeenpwned.com/api/breaches"
if(!is.null(domain)) URLS<- urltools::param_set(URLS,"domain",urltools::url_encode(domain))

res<- GETcontent(URLS, HIBP_headers(...))
if(identical(res,list())) res<-data.frame(Title=NA,Name=NA,Domain=domain,
BreachDate=NA,AddedDate=NA,PwnCount=NA,
Description=NA,DataClasses=NA,IsVerified=NA,
IsSensitive=NA,LogoType=NA )
return(res)

}
23 changes: 23 additions & 0 deletions man/breached_sites.Rd

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

3 changes: 3 additions & 0 deletions tests/testthat/test-account_breaches.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ test_that("account_breaches works for multiple accounts",{
expect_gte(ncol(res[[a]]),1) # key test
expect_gte(nrow(res[[a]]),1)
}

# With domain (no breach)
res<-account_breaches(acct, domain = "xyz.com")

Expand All @@ -101,6 +102,8 @@ test_that("account_breaches works for multiple accounts",{
expect_gte(nrow(res[[a]]),1)
}

# With header passthrough
expect_error(account_breaches(acct,agent="blah"),NA)
})

test_that("handles incorrect values",{
Expand Down
39 changes: 39 additions & 0 deletions tests/testthat/test-breached_sites.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
context("breached-sites")

test_that("breached_sites works",{

# Simplest usage scenario
res<-breached_sites()

expect_is(res, "data.frame")
expect_gt(ncol(res),1)
expect_gt(nrow(res),1)

# With domain (known breach)
res<-breached_sites(domain = "adobe.com")

expect_is(res, "data.frame")
expect_gt(ncol(res),1)
expect_equal(nrow(res),1)

# With domain (no breach)
res<-breached_sites(domain = "xyz.com")

expect_is(res, "data.frame")
expect_gt(ncol(res),1)
expect_equal(nrow(res),1)

# With header passthrough
expect_error(breached_sites(agent="blah"),NA)

})


test_that("handles incorrect values",{
errors<-"Problematic domain"

expect_error(breached_sites(TRUE),errors)
expect_error(breached_sites(as.character(c())),errors)
expect_error(breached_sites(rep(errors,2)),errors)

})

0 comments on commit 5419a30

Please sign in to comment.