-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Translate DNS mock test to integration testsuite
- Loading branch information
Showing
6 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Test.DNSMock where | ||
|
||
import Control.Lens | ||
import Control.Monad.Reader.Class | ||
import qualified Data.ByteString.Lazy as LBS | ||
import Data.String.Conversions (cs) | ||
import Network.DNS | ||
import Network.DNS.Decode as Dec | ||
import qualified Network.HTTP.Client as HTTP | ||
import Testlib.Prelude | ||
|
||
type LByteString = LBS.ByteString | ||
|
||
-- | Test that we can provide test date (a TXT record) in Technitium | ||
-- (dns-server for tests) | ||
testNewTXTRecord :: (HasCallStack) => App () | ||
testNewTXTRecord = do | ||
env <- ask | ||
let dohUrl = "http://localhost:80/dns-query" | ||
apiUrl = "http://" <> env.dnsMockServerConfig.host <> ":" <> show env.dnsMockServerConfig.apiPort | ||
|
||
-- api stuff | ||
do | ||
-- get api key | ||
tok :: String <- do | ||
let url = apiUrl <> "/user/createToken" | ||
req <- externalRequest url <&> addQueryParams [("user", "admin"), ("pass", "admin"), ("tokenName", "someToken")] | ||
bindResponse (submit "POST" req) $ \resp -> do | ||
resp.status `shouldMatchInt` 200 | ||
resp.jsonBody %. "status" `shouldMatch` ("ok" :: String) | ||
asString $ resp.jsonBody %. "token" | ||
|
||
-- add 0.0.0.0/0 to ACL () | ||
do | ||
let url = apiUrl <> "/settings/set" | ||
req <- externalRequest url <&> addQueryParams [("token", tok), ("reverseProxyNetworkACL", "0.0.0.0/0")] | ||
submit "POST" req >>= assertStatus 200 | ||
|
||
-- register zone | ||
do | ||
let url = apiUrl <> "/zones/create" | ||
req <- externalRequest url <&> addQueryParams [("token", tok), ("zone", "example.com"), ("type", "primary")] | ||
submit "POST" req >>= assertStatus 200 | ||
|
||
-- register txt record in zone | ||
do | ||
let url = apiUrl <> "/zones/records/add" | ||
params = | ||
[ ("token", tok), | ||
("zone", "example.com"), | ||
("domain", "example.com"), | ||
("type", "TXT"), | ||
("text", "hallo, welt!") | ||
] | ||
req <- externalRequest url <&> addQueryParams params | ||
submit "POST" req >>= assertStatus 200 | ||
|
||
-- ask the dns question and check response | ||
do | ||
let question = HTTP.RequestBodyBS $ encodeQuestion 0 (Question "example.com" TXT) mempty | ||
headers = | ||
[ ("Content-Type", "application/dns-message"), | ||
("Accept", "application/dns-message") | ||
] | ||
req <- externalRequest dohUrl <&> addBody question "application/dns-message" . addHeaders headers | ||
bindResponse (submit "POST" req) $ \resp -> do | ||
let received = Dec.decode (resp.body :: ByteString) | ||
expected = Right (DNSMessage {header = DNSHeader {identifier = 0, flags = DNSFlags {qOrR = QR_Response, opcode = OP_STD, authAnswer = True, trunCation = False, recDesired = True, recAvailable = True, rcode = NoErr, authenData = False, chkDisable = False}}, ednsHeader = EDNSheader (EDNS {ednsVersion = 0, ednsUdpSize = 1232, ednsDnssecOk = False, ednsOptions = []}), question = [Question {qname = "example.com.", qtype = TXT}], answer = [ResourceRecord {rrname = "example.com.", rrtype = TXT, rrclass = 1, rrttl = 3600, rdata = RD_TXT "hallo, welt!"}], authority = [], additional = []}) | ||
assertBool "Expected DNS response does match" (received == expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters