|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +{-# LANGUAGE TemplateHaskell #-} |
| 3 | +module GitHub.PublicSSHKeysSpec where |
| 4 | + |
| 5 | +import GitHub |
| 6 | + (Auth (..), FetchCount (..), PublicSSHKey (..), executeRequest) |
| 7 | +import GitHub.Endpoints.Users.PublicSSHKeys |
| 8 | + (publicSSHKey', publicSSHKeys', publicSSHKeysForR) |
| 9 | + |
| 10 | +import Data.Either.Compat (isRight) |
| 11 | +import Data.String (fromString) |
| 12 | +import System.Environment (lookupEnv) |
| 13 | +import Test.Hspec (Spec, describe, it, pendingWith, shouldSatisfy) |
| 14 | + |
| 15 | +import qualified Data.Vector as V |
| 16 | + |
| 17 | +fromRightS :: Show a => Either a b -> b |
| 18 | +fromRightS (Right b) = b |
| 19 | +fromRightS (Left a) = error $ "Expected a Right and got a Left" ++ show a |
| 20 | + |
| 21 | +withAuth :: (Auth -> IO ()) -> IO () |
| 22 | +withAuth action = do |
| 23 | + mtoken <- lookupEnv "GITHUB_TOKEN" |
| 24 | + case mtoken of |
| 25 | + Nothing -> pendingWith "no GITHUB_TOKEN" |
| 26 | + Just token -> action (OAuth $ fromString token) |
| 27 | + |
| 28 | +spec :: Spec |
| 29 | +spec = do |
| 30 | + describe "publicSSHKeysFor'" $ do |
| 31 | + it "works" $ withAuth $ \auth -> do |
| 32 | + keys <- executeRequest auth $ publicSSHKeysForR "phadej" FetchAll |
| 33 | + V.length (fromRightS keys) `shouldSatisfy` (> 1) |
| 34 | + |
| 35 | + describe "publicSSHKeys' and publicSSHKey'" $ do |
| 36 | + it "works" $ withAuth $ \auth -> do |
| 37 | + keys <- publicSSHKeys' auth |
| 38 | + V.length (fromRightS keys) `shouldSatisfy` (> 1) |
| 39 | + |
| 40 | + key <- publicSSHKey' auth (publicSSHKeyId $ V.head (fromRightS keys)) |
| 41 | + key `shouldSatisfy` isRight |
0 commit comments