Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
DCSG2900-ThreatTotal
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Johannes Barstad
DCSG2900-ThreatTotal
Commits
15ae8de7
Commit
15ae8de7
authored
3 years ago
by
Jonas Kjærandsen
Browse files
Options
Downloads
Patches
Plain Diff
Replaced status codes with constants in the main test, also changed the error messages.
parent
2e4ae360
No related branches found
No related tags found
1 merge request
!2
Merge react-branch into main.
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main_test.go
+27
-30
27 additions, 30 deletions
main_test.go
with
27 additions
and
30 deletions
main_test.go
+
27
−
30
View file @
15ae8de7
...
...
@@ -12,7 +12,7 @@ import (
/**
* API - Test to check if the URL intelligence endpoint returns HTTP StatusOK when expected
*/
*/
func
TestUrlIntelligenceOK
(
t
*
testing
.
T
)
{
...
...
@@ -38,14 +38,14 @@ func TestUrlIntelligenceOK(t *testing.T) {
}
defer
res
.
Body
.
Close
()
if
res
.
StatusCode
!=
200
{
if
res
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatalf
(
"Test failed, error code different from expected 200, received: %d"
,
res
.
StatusCode
)
}
}
/**
* API test to check whether the url-intelligence endpoint will return 401 Unauthorized when attempting to be accessed without log in.
*/
*/
func
TestUrlIntelligenceUnauthorized
(
t
*
testing
.
T
)
{
...
...
@@ -66,16 +66,15 @@ func TestUrlIntelligenceUnauthorized(t *testing.T) {
}
defer
res
.
Body
.
Close
()
if
res
.
StatusCode
!=
401
{
if
res
.
StatusCode
!=
http
.
StatusUnauthorized
{
t
.
Fatalf
(
"Test failed, error code different from expected 401, received: %d"
,
res
.
StatusCode
)
}
}
/**
* API - Test to check if the hash intelligence endpoint returns HTTP StatusOK when expected
*/
func
TestHashIntelligenceOK
(
t
*
testing
.
T
){
*/
func
TestHashIntelligenceOK
(
t
*
testing
.
T
)
{
content
,
err
:=
ioutil
.
ReadFile
(
"testauth.txt"
)
if
err
!=
nil
{
...
...
@@ -99,15 +98,15 @@ func TestHashIntelligenceOK(t *testing.T){
}
defer
res
.
Body
.
Close
()
if
res
.
StatusCode
!=
200
{
t
.
Fatalf
(
"Test failed,
error
code different from expected 200, received: %d"
,
res
.
StatusCode
)
if
res
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatalf
(
"Test failed,
status
code different from expected 200, received: %d"
,
res
.
StatusCode
)
}
}
/**
* API test to check whether the hash-intelligence endpoint will return 401 Unauthorized when attempting to be accessed without log in.
*/
func
TestHashIntelligenceUnauthorized
(
t
*
testing
.
T
){
*/
func
TestHashIntelligenceUnauthorized
(
t
*
testing
.
T
)
{
auth
:=
"ThisShouldNotWork"
url
:=
"http://localhost:8081/hash-intelligence?hash=a7a665a695ec3c0f862a0d762ad55aff6ce6014359647e7c7f7e3c4dc3be81b7&userAuth="
+
auth
...
...
@@ -125,8 +124,8 @@ func TestHashIntelligenceUnauthorized(t *testing.T){
}
defer
res
.
Body
.
Close
()
if
res
.
StatusCode
!=
401
{
t
.
Fatalf
(
"Test failed,
error
code different from expected 401, received: %d"
,
res
.
StatusCode
)
if
res
.
StatusCode
!=
http
.
StatusUnauthorized
{
t
.
Fatalf
(
"Test failed,
status
code different from expected 401, received: %d"
,
res
.
StatusCode
)
}
}
...
...
@@ -141,7 +140,7 @@ func TestHashIntelligenceUnauthorized(t *testing.T){
* If there is a screenshot of the requested URL
* If status or content is not set in any of the responses from the intelligence sources
*
*/
*/
func
TestUrlIntelligenceValidOutput
(
t
*
testing
.
T
)
{
content
,
err
:=
ioutil
.
ReadFile
(
"testauth.txt"
)
...
...
@@ -166,11 +165,10 @@ func TestUrlIntelligenceValidOutput(t *testing.T) {
}
defer
res
.
Body
.
Close
()
if
res
.
StatusCode
!=
200
{
t
.
Fatalf
(
"Test failed,
error
code different from expected 200, received: %d"
,
res
.
StatusCode
)
if
res
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatalf
(
"Test failed,
status
code different from expected 200, received: %d"
,
res
.
StatusCode
)
}
body
,
err
:=
ioutil
.
ReadAll
(
res
.
Body
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error: reading api response"
)
...
...
@@ -191,7 +189,7 @@ func TestUrlIntelligenceValidOutput(t *testing.T) {
t
.
Fatalf
(
"Error in screenshot"
)
}
for
i
:=
0
;
i
<=
3
;
i
++
{
for
i
:=
0
;
i
<=
3
;
i
++
{
if
jsonResponse
.
FrontendResponse
[
i
]
.
EN
.
Status
==
""
{
t
.
Fatalf
(
"One status or more statuses are not set in english. Sourcename: %s , content is empty"
,
jsonResponse
.
FrontendResponse
[
i
]
.
SourceName
)
}
...
...
@@ -200,10 +198,10 @@ func TestUrlIntelligenceValidOutput(t *testing.T) {
}
}
for
i
:=
0
;
i
<=
3
;
i
++
{
for
i
:=
0
;
i
<=
3
;
i
++
{
if
jsonResponse
.
FrontendResponse
[
i
]
.
EN
.
Content
==
""
{
t
.
Fatalf
(
"One content or more contents are not set in english. Sourcename: %s , content is empty"
,
jsonResponse
.
FrontendResponse
[
i
]
.
SourceName
)
}
if
jsonResponse
.
FrontendResponse
[
i
]
.
NO
.
Content
==
""
{
t
.
Fatalf
(
"One status or more contents are not set in norwegian. Sourcename: %s is not set."
,
jsonResponse
.
FrontendResponse
[
i
]
.
SourceName
)
...
...
@@ -222,7 +220,7 @@ func TestUrlIntelligenceValidOutput(t *testing.T) {
* If the first and second sourceName is "Hybrid Analysis and AlienVault" respectively, as expected
* If status or content is not set in any of the responses from the intelligence sources both in english and norwegian.
* If the status of AlienVault is risk as expected.
*/
*/
func
TestHash_IntelligenceValidOutput
(
t
*
testing
.
T
)
{
content
,
err
:=
ioutil
.
ReadFile
(
"testauth.txt"
)
...
...
@@ -251,7 +249,6 @@ func TestHash_IntelligenceValidOutput(t *testing.T) {
t
.
Fatalf
(
"Test failed, error code different from expected 200, received: %d"
,
res
.
StatusCode
)
}
body
,
err
:=
ioutil
.
ReadAll
(
res
.
Body
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error: reading api response"
)
...
...
@@ -268,11 +265,11 @@ func TestHash_IntelligenceValidOutput(t *testing.T) {
t
.
Fatalf
(
"The first sourcename is not expected Google Safebrowsing API, output: %s"
,
jsonResponse
.
FrontendResponse
[
0
]
.
SourceName
)
}
if
jsonResponse
.
FrontendResponse
[
1
]
.
SourceName
!=
"AlienVault"
{
if
jsonResponse
.
FrontendResponse
[
1
]
.
SourceName
!=
"AlienVault"
{
t
.
Fatalf
(
"Unexpected sourcename expected AlienVault, reality: %s"
,
jsonResponse
.
FrontendResponse
[
1
]
.
SourceName
)
}
for
i
:=
0
;
i
<=
1
;
i
++
{
for
i
:=
0
;
i
<=
1
;
i
++
{
if
jsonResponse
.
FrontendResponse
[
i
]
.
EN
.
Status
==
""
{
t
.
Fatalf
(
"One status or more statuses are not set in english, Sourcename: %s is not set."
,
jsonResponse
.
FrontendResponse
[
i
]
.
SourceName
)
}
...
...
@@ -281,7 +278,7 @@ func TestHash_IntelligenceValidOutput(t *testing.T) {
}
}
for
i
:=
0
;
i
<=
1
;
i
++
{
for
i
:=
0
;
i
<=
1
;
i
++
{
if
jsonResponse
.
FrontendResponse
[
i
]
.
EN
.
Content
==
""
{
t
.
Fatalf
(
"One status or more contents are not set in english, Sourcename: %s is not set."
,
jsonResponse
.
FrontendResponse
[
i
]
.
SourceName
)
}
...
...
@@ -290,17 +287,17 @@ func TestHash_IntelligenceValidOutput(t *testing.T) {
}
}
if
jsonResponse
.
FrontendResponse
[
1
]
.
EN
.
Status
!=
"Risk"
{
if
jsonResponse
.
FrontendResponse
[
1
]
.
EN
.
Status
!=
"Risk"
{
t
.
Fatalf
(
"The status of AlienVault has is not as expected Risk, Status is: %s"
,
jsonResponse
.
FrontendResponse
[
1
]
.
EN
.
Status
)
}
}
/**
* This API test checks if an unspecified endpoint in the API returns 404 as expected
* This API test checks if an unspecified endpoint in the API returns 404 as expected
*
*/
*/
func
TestNotSpecifiedEndpoint
(
t
*
testing
.
T
){
func
TestNotSpecifiedEndpoint
(
t
*
testing
.
T
)
{
url
:=
"http://localhost:8081/ThisShouldNotExist"
...
...
@@ -321,4 +318,4 @@ func TestNotSpecifiedEndpoint(t *testing.T){
if
res
.
StatusCode
!=
http
.
StatusNotFound
{
t
.
Fatalf
(
"Staus code did not return 404 as expected, code returned %d"
,
res
.
StatusCode
)
}
}
\ No newline at end of file
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment