2020-09-07 06:56:18 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import CodableBloomFilter
|
|
|
|
import Combine
|
|
|
|
import CombineExpectations
|
|
|
|
@testable import ServiceLayer
|
|
|
|
@testable import ServiceLayerMocks
|
|
|
|
import Stubbing
|
|
|
|
import XCTest
|
|
|
|
|
2020-09-10 06:51:31 +02:00
|
|
|
class InstanceURLServiceTests: XCTestCase {
|
2020-09-07 06:56:18 +02:00
|
|
|
func testFiltering() throws {
|
2020-09-10 06:51:31 +02:00
|
|
|
let sut = InstanceURLService(environment: .mock())
|
2020-09-07 06:56:18 +02:00
|
|
|
let unfilteredInstanceURL = URL(string: "https://unfiltered.instance")!
|
|
|
|
let filteredInstanceURL = URL(string: "https://filtered.instance")!
|
|
|
|
let subdomainFilteredInstanceURL = URL(string: "https://subdomain.filtered.instance")!
|
|
|
|
|
|
|
|
XCTAssertFalse(sut.isFiltered(url: unfilteredInstanceURL))
|
|
|
|
XCTAssertTrue(sut.isFiltered(url: filteredInstanceURL))
|
|
|
|
XCTAssertTrue(sut.isFiltered(url: subdomainFilteredInstanceURL))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUpdating() throws {
|
|
|
|
let environment = AppEnvironment.mock()
|
2020-09-10 06:51:31 +02:00
|
|
|
var sut = InstanceURLService(environment: environment)
|
2020-09-07 06:56:18 +02:00
|
|
|
let previouslyFilteredInstanceURL = URL(string: "https://filtered.instance")!
|
|
|
|
let newlyFilteredInstanceURL = URL(string: "https://instance.filtered")!
|
|
|
|
|
|
|
|
XCTAssertTrue(sut.isFiltered(url: previouslyFilteredInstanceURL))
|
|
|
|
XCTAssertFalse(sut.isFiltered(url: newlyFilteredInstanceURL))
|
|
|
|
|
2020-09-08 08:49:58 +02:00
|
|
|
var updatedFilter = try BloomFilter<String>(hashes: [.djb232, .sdbm32], byteCount: 16)
|
2020-09-07 06:56:18 +02:00
|
|
|
|
|
|
|
updatedFilter.insert("instance.filtered")
|
|
|
|
|
|
|
|
let updatedFilterData = try JSONEncoder().encode(updatedFilter)
|
|
|
|
let stub: HTTPStub = .success((URLResponse(), updatedFilterData))
|
|
|
|
|
|
|
|
StubbingURLProtocol.setStub(stub, forURL: URL(string: "https://filter.metabolist.com/filter.json")!)
|
|
|
|
|
|
|
|
let updateRecorder = sut.updateFilter().collect().record()
|
|
|
|
|
|
|
|
_ = try wait(for: updateRecorder.next(), timeout: 1)
|
|
|
|
|
|
|
|
XCTAssertFalse(sut.isFiltered(url: previouslyFilteredInstanceURL))
|
|
|
|
XCTAssertTrue(sut.isFiltered(url: newlyFilteredInstanceURL))
|
|
|
|
|
2020-09-10 06:51:31 +02:00
|
|
|
sut = InstanceURLService(environment: environment)
|
2020-09-07 06:56:18 +02:00
|
|
|
|
|
|
|
XCTAssertFalse(sut.isFiltered(url: previouslyFilteredInstanceURL))
|
|
|
|
XCTAssertTrue(sut.isFiltered(url: newlyFilteredInstanceURL))
|
|
|
|
}
|
|
|
|
}
|